我有點困惑,我已經從其他地方讀過,超時/間隔是每隔x秒運行一次Javascript的最佳方式。我必須這樣做,以便我的函數每1秒運行一次,因爲這是我使用的API的規則。
我的代碼如下:
$.each(match_details_json.result.players, function(index, v){
if (v.account_id == 38440257){ //finds desired playerid
var match_id_2 = matches[i];
hero = v.hero_id;
playerpos = v.player_slot;
var kills = v.kills;
var deaths = v.deaths;
var assists = v.assists;
var kda = ""+kills+"/"+deaths+"/"+assists+"";
console.log(match_id_2, hero, playerpos, result, gamemode, kda);
callback();
console.log(match_id_2, hero, result, gamemode, kda);
h=h+1;
setTimeout(get_match_details(h), 10000);
i=i+1;
}
else{
console.log('Not desired player, skipping...');
}
});
很多亂碼出現。但重要的部分是setTimeout(get_match_details(h), 10000);
無論這是否正確,那就是我試圖說「在10秒內再次運行此功能」並繼續,直到每個方法完成。它不起作用。
如果有必要,這是我get_match_details
功能:
function get_match_details(){
$.ajax({
url: 'php/ApiMatchPull.php',
data: {accountid:'38440257', querytype:'GetMatchDetails', querycondition1:'match_id='+matches[h]},
success: function (data) {
console.log ('Match Details were was pulled successfully');
match_details_json = data;
matchdetailsfill(checkvalidity);
}
});
}
預先感謝您。