开发者

delay ajax request

开发者 https://www.devze.com 2023-02-12 04:26 出处:网络
What is the best way to set timeout that will force my page no开发者_运维知识库t to set more than one ajax request per minute

What is the best way to set timeout that will force my page no开发者_运维知识库t to set more than one ajax request per minute

Thanks in advance!


I'd use a simple variable holding the timerId returned from setTimeout. Something like this:

var timerId;
var doRequest = function(){
  if(timerId) return;
  timerId = setTimeout(doRequest, 60*1000);
  // ajax code here
};

This will throttle the request to once per minute. If you need to stop the setTimeout call, then simply add another boolean based off the user event you are watching for.

0

精彩评论

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