Javascript:在特定时间段后调用函数

时间:2023-02-09 01:21:12

In JavaScript, How can I call a function after a specific time interval?

在JavaScript中,如何在特定时间间隔后调用函数?

Here is my function I want to run:

这是我想要运行的函数:

function FetchData() {
}

4 个解决方案

#1


109  

You can use JavaScript Timing Events to call function after certain interval of time:

您可以使用JavaScript Timing Events在一段时间后调用函数:

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.i.e中使用两种时间事件方法。

  1. setInterval(): executes a function, over and over again, at specified time intervals
  2. setInterval():以指定的时间间隔一遍又一遍地执行一个函数
  3. setTimeout() : executes a function, once, after waiting a specified number of milliseconds
  4. setTimeout():在等待指定的毫秒数后执行一次函数

#2


11  

Execute function FetchData() once after 1000 milliseconds:

1000毫秒后执行一次函数FetchData():

setTimeout(FetchData,1000);

Execute function FetchData() repeatedly every 1000 milliseconds:

每1000毫秒重复执行一次函数FetchData():

setInterval(FetchData,1000);

#3


8  

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。

#4


0  

setTimeout(func, 5000);

setTimeout(func,5000);

-- it will call the function named func() after the time specified. here, 5000 milli seconds , i.e) after 5 seconds

- 它会在指定的时间后调用名为func()的函数。这里,5000毫秒,即5秒后

#1


109  

You can use JavaScript Timing Events to call function after certain interval of time:

您可以使用JavaScript Timing Events在一段时间后调用函数:

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.i.e中使用两种时间事件方法。

  1. setInterval(): executes a function, over and over again, at specified time intervals
  2. setInterval():以指定的时间间隔一遍又一遍地执行一个函数
  3. setTimeout() : executes a function, once, after waiting a specified number of milliseconds
  4. setTimeout():在等待指定的毫秒数后执行一次函数

#2


11  

Execute function FetchData() once after 1000 milliseconds:

1000毫秒后执行一次函数FetchData():

setTimeout(FetchData,1000);

Execute function FetchData() repeatedly every 1000 milliseconds:

每1000毫秒重复执行一次函数FetchData():

setInterval(FetchData,1000);

#3


8  

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。

#4


0  

setTimeout(func, 5000);

setTimeout(func,5000);

-- it will call the function named func() after the time specified. here, 5000 milli seconds , i.e) after 5 seconds

- 它会在指定的时间后调用名为func()的函数。这里,5000毫秒,即5秒后