连续每5秒调用一个javascript函数[重复]

时间:2022-10-28 01:24:11

Possible Duplicate:
Calling a function every 60 seconds

可能重复:每60秒调用一个函数

call a javascript function every 5 seconds continuously. I have seen the setTimeOut event. will it be working fine if i want it continuously?

连续每5秒调用一个javascript函数。我已经看到了setTimeOut事件。如果我一直想要它,它能正常工作吗?

5 个解决方案

#1


115  

You can use setInterval(), the arguments are the same.

可以使用setInterval(),参数是相同的。

setInterval(function() {
  // method to be executed;
}, 5000);

#2


61  

Do a "recursive" setTimeout of your function, and it will keep being executed every amount of time defined:

执行函数的“递归”setTimeout,它将在定义的每一段时间内持续执行:

function yourFunction(){
    // do whatever you like here

    setTimeout(yourFunction, 5000);
}

yourFunction();

#3


18  

As best coding practices suggests, use setTimeout instead of setInterval.

正如最佳编码实践所建议的,使用setTimeout而不是setInterval。

function foo() {

    // your function code here

    setTimeout(foo, 5000);
}

foo();

Please note that this is NOT a recursive function. The function is not calling itself before it ends, it's calling a setTimeout function that will be later call the same function again.

请注意,这不是递归函数。函数在结束之前没有调用自己,而是调用一个setTimeout函数,这个函数稍后将再次调用相同的函数。

#4


14  

For repeating an action in the future, there is the built in setInterval function that you can use instead of setTimeout.
It has a similar signature, so the transition from one to another is simple:

为了在将来重复操作,您可以使用内置的setInterval函数来代替setTimeout。它有一个相似的签名,所以从一个签名到另一个签名的转换很简单:

setInterval(function() {
    // do stuff
}, duration);

#5


4  

Good working example here: http://jsfiddle.net/MrTest/t4NXD/62/

这里有一个很好的工作示例:http://jsfiddle.net/MrTest/t4NXD/62/。

Plus:

加:

  • has nice fade in / fade out animation
  • 动画是否有好的淡入/淡入
  • will pause on :hover
  • 将暂停:徘徊
  • will prevent running multiple actions (finish run animation before starting second)
  • 将防止运行多个操作(在启动第二个操作之前完成运行动画)
  • will prevent going broken when in the tab ( browser stops scripts in the tabs)
  • 将防止在选项卡中出错(浏览器停止选项卡中的脚本)

Tested and working!

测试和工作!

#1


115  

You can use setInterval(), the arguments are the same.

可以使用setInterval(),参数是相同的。

setInterval(function() {
  // method to be executed;
}, 5000);

#2


61  

Do a "recursive" setTimeout of your function, and it will keep being executed every amount of time defined:

执行函数的“递归”setTimeout,它将在定义的每一段时间内持续执行:

function yourFunction(){
    // do whatever you like here

    setTimeout(yourFunction, 5000);
}

yourFunction();

#3


18  

As best coding practices suggests, use setTimeout instead of setInterval.

正如最佳编码实践所建议的,使用setTimeout而不是setInterval。

function foo() {

    // your function code here

    setTimeout(foo, 5000);
}

foo();

Please note that this is NOT a recursive function. The function is not calling itself before it ends, it's calling a setTimeout function that will be later call the same function again.

请注意,这不是递归函数。函数在结束之前没有调用自己,而是调用一个setTimeout函数,这个函数稍后将再次调用相同的函数。

#4


14  

For repeating an action in the future, there is the built in setInterval function that you can use instead of setTimeout.
It has a similar signature, so the transition from one to another is simple:

为了在将来重复操作,您可以使用内置的setInterval函数来代替setTimeout。它有一个相似的签名,所以从一个签名到另一个签名的转换很简单:

setInterval(function() {
    // do stuff
}, duration);

#5


4  

Good working example here: http://jsfiddle.net/MrTest/t4NXD/62/

这里有一个很好的工作示例:http://jsfiddle.net/MrTest/t4NXD/62/。

Plus:

加:

  • has nice fade in / fade out animation
  • 动画是否有好的淡入/淡入
  • will pause on :hover
  • 将暂停:徘徊
  • will prevent running multiple actions (finish run animation before starting second)
  • 将防止运行多个操作(在启动第二个操作之前完成运行动画)
  • will prevent going broken when in the tab ( browser stops scripts in the tabs)
  • 将防止在选项卡中出错(浏览器停止选项卡中的脚本)

Tested and working!

测试和工作!