gpt4 book ai didi

javascript - 我的 setTimeout 不尊重延迟

转载 作者:行者123 更新时间:2023-11-28 12:37:30 26 4
gpt4 key购买 nike

我想使用 setTimeout 每 10 秒重复一次函数。我的职能是:

dateInLive = function() {
crono = function(){
setTimeout(function() {
$('.datePoste').each(function(){
$(this).load('in_live.php','in_live='+$(this).attr('id'))
});
crono();
}
,10000);
}
crono();
}

但是,这确实是随机的;有时15秒后重复,有时3秒后,有时6秒后重复。

最佳答案

仅当所有ajax请求完成时才调用crono():

function crono(){
setTimeout(function() {
var arr = [];
$('.datePoste').each(function(){
var self = this;
xhr = $.get('in_live.php', {in_live : this.id}, function(data) {
$(self).html( $.parseHTML(data) );
});
arr.push(xhr);
});
$.when.apply($, arr).done(crono);
}, 10000);
}

关于javascript - 我的 setTimeout 不尊重延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15641417/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com