Javascript:在特定时间段后调用函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11901074/
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
Javascript: Call a function after specific time period
提问by Shaggy
In JavaScript, How can I call a function after a specific time interval?
在 JavaScript 中,如何在特定时间间隔后调用函数?
Here is my function I want to run:
这是我要运行的函数:
function FetchData() {
}
回答by Akash KC
You can use JavaScript Timing Eventsto call function after certain interval of time:
您可以使用JavaScript 计时事件在一定时间间隔后调用函数:
This shows the alert box after 3 seconds:
这将在 3 秒后显示警告框:
setInterval(function(){alert("Hello")},3000);
You can use two method of time event in javascript.i.e.
您可以在 javascript.ie 中使用两种时间事件方法
setInterval()
: executes a function, over and over again, at specified time intervalssetTimeout()
: executes a function, once, after waiting a specified number of milliseconds
setInterval()
: 以指定的时间间隔一遍又一遍地执行一个函数setTimeout()
: 在等待指定的毫秒数后执行一次函数
回答by Mark
Execute function FetchData()
onceafter 1000 milliseconds:
1000 毫秒后执行FetchData()
一次函数:
setTimeout(FetchData,1000);
Execute function FetchData()
repeatedlyevery 1000 milliseconds:
每 1000 毫秒FetchData()
重复执行一次函数:
setInterval(FetchData,1000);
回答by oezi
sounds like you're looking for setInterval. It's as easy as this:
听起来你正在寻找setInterval。就这么简单:
function FetchData() {
// do something
}
setInterval(FetchData, 60000);
if you only want to call something once, theres setTimeout.
如果你只想调用一次,那就是setTimeout。
回答by Maxx Selva K
setTimeout(func, 5000);
设置超时(功能,5000);
-- it will call the function named func() after the time specified. here, 5000 milli seconds , i.e) after 5 seconds
-- 它会在指定的时间之后调用名为 func() 的函数。这里,5000 毫秒,即)5 秒后