我需要进行大量的函数调用,并且我想将它们错开,以便服务器负载不会受到太大影响。
基本上我的网站有8000多名会员。我需要遍历每个成员,并为每个成员运行一系列技能(总共30个)并触发一个外部URL,对每个技能执行每个成员的检查。
这样的事情:
var skills = ['56','79', '92', '118', '129', '134', '363', '364', '365', '367', '362', '368', '66', '84', '120', '123', '136', '359', '137', '142', '155', '176', '165', '168', '171', '174'];
for ( i = 8175; i < 8176; i++){
setInterval( function (){
$.each(skills, function (index, value){
$( "#result" ).load( "/accomplishments/" + value + '/' + i, function( response, status, xhr ) {
if ( status == "success" ) {
var msg = "<p>User: " + i + ' checked for skill ' + value + '</p>';
$( "#messages" ).append(msg);
};
});
});
}, 5000);
}
有人能建议最好的方法吗?感谢。
答案 0 :(得分:0)
看起来您希望以5秒为增量运行代码8175次。
您的代码应该更像这样:
var skills = ['56','79', '92', '118', '129', '134', '363', '364', '365', '367', '362', '368', '66', '84', '120', '123', '136', '359', '137', '142', '155', '176', '165', '168', '171', '174'];
var memberId = 1; // assuming id 0 is invalid, we'll start counting on 1
var totalMembers = 8175; // we'll run the code for id 8175, then stop
var intervalId = setInterval(function() {
$.each(skills, function (index, value){
$( "#result" ).load( "/accomplishments/" + value + '/' + memberId, function( response, status, xhr ) {
if ( status == "success" ) {
var msg = "<p>User: " + memberId + ' checked for skill ' + value + '</p>';
$( "#messages" ).append(msg);
};
});
});
memberId += 1;
if( memberId > totalMembers ) {
clearInterval(intervalId);
}
}, 5000);