我有我的网页下面的脚本:的JavaScript停止间隔功能异步
tid = setInterval(checkBounty, 1000);
function checkBounty(){
var elapsed = (Date.now()/1000) - parseInt($(".bounty").data('created')) ;
$(".remaining-time").html(valid - elapsed); //update remaining time until bounty expires
if (elapsed> valid){
$.POST('', {id: id}, function(){
console.log ('bounty cancelled');
clearInterval(tid);
});
//do an ajax post to cancel the bounty;
}
}
这是因为它在执行异步阿贾克斯后多次触发。我怎样才能避免这种情况?
编辑
我更新了我用的是代码的问题,忘了补充调用clearInterval。我现在意识到这是ajax在一秒钟内没有响应,并且函数被再次调用。
它触发多个Ajax请求不是因为它异步执行,而是因为它是异步执行的这是其行为 – micnic 2013-04-20 11:18:09