javascript 在特定时间后加载 url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9186574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:52:09  来源:igfitidea点击:

load a url after a specific amount of time

javascriptjquery

提问by rmagnum2002

I have a single url that I need to open it after a specific amount of time.

我有一个 url,需要在特定时间后打开它。

I think this can be done using js/jquery and I see it working like this: I open the page created with the js code inside and the url loads automatic in a new tab and after 5 minutes this same url is opened in a new tab or in the same tab that was previously opened. It's a trick I am working on and the problem is that I don't know much in js/jquery that's why I need your help.

我认为这可以使用 js/jquery 来完成,我看到它是这样工作的:我打开用里面的 js 代码创建的页面,url 在新选项卡中自动加载,5 分钟后在新选项卡中打开相同的 url或在先前打开的同一选项卡中。这是我正在研究的一个技巧,问题是我对 js/jquery 了解不多,这就是为什么我需要你的帮助。

You might ask me: Hey, this code will open a new tab each 5 minutes so you'll have a lot of them running, are you sure you need this?.. and I say yes, that's what I need, a new tab for a single url each 5 minutes.

你可能会问我:嘿,这个代码每 5 分钟会打开一个新标签,所以你会运行很多,你确定你需要这个吗?..我说是的,这就是我需要的,一个新标签每 5 分钟一个 url。

Thanks.

谢谢。

回答by Gabe

You could use setTimeout()which will wait a specified amount of time (ms) then execute the declared function.

您可以使用setTimeout()which 将等待指定的时间(ms)然后执行声明的函数。

Example:

例子:

setTimeout(openUrl, 5000); // Wait 5 seconds

function openUrl(){

   window.open('http://google.com');

}

To repeat an action on a timer you can use setInterval()

要在计时器上重复操作,您可以使用 setInterval()

setInterval(openUrl, 5000);

回答by Christopher Johnson

回答by sureshvv

You need to call settimeout inside openUrl again to repeat it endlessly

你需要再次调用 openUrl 里面的 settimeout 来无休止地重复它