开发者

How do I delay a function call for 5 seconds? [duplicate]

开发者 https://www.devze.com 2023-02-05 07:30 出处:网络
This question already has answers here: Sleep in JavaScript - delay between actions (15 answers) Closed 7 years ago.
This question already has answers here: Sleep in JavaScript - delay between actions (15 answers) Closed 7 years ago.

I want widget.Rotator.rotate() to be delayed 5 seconds开发者_如何学Python between calls... how do I do this in jQuery... it seems like jQuery's delay() wouldn't work for this...


You can use plain javascript, this will call your_func once, after 5 seconds:

setTimeout(function() { your_func(); }, 5000);

If your function has no parameters and no explicit receiver you can call directly setTimeout(func, 5000)

There is also a plugin I've used once. It has oneTime and everyTime methods.

  • jQuery timers plugin


var rotator = function(){
  widget.Rotator.rotate();
  setTimeout(rotator,5000);
};
rotator();

Or:

setInterval(
  function(){ widget.Rotator.rotate() },
  5000
);

Or:

setInterval(
  widget.Rotator.rotate.bind(widget.Rotator),
  5000
);
0

精彩评论

暂无评论...
验证码 换一张
取 消