Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript setTimeout/Interval从$内调用。每个方法?_Javascript_Jquery - Fatal编程技术网

Javascript setTimeout/Interval从$内调用。每个方法?

Javascript setTimeout/Interval从$内调用。每个方法?,javascript,jquery,Javascript,Jquery,我有点困惑,我从其他地方读到,timeout/interval是让Javascript函数每x秒运行一次的最佳方法。我必须使我的函数每1秒运行一次,因为这是我使用的API的规则 我的代码如下: $.each(match_details_json.result.players, function(index, v){ if (v.account_id == 38440257){ //finds desired playerid var match_id_2 = matches[

我有点困惑,我从其他地方读到,timeout/interval是让Javascript函数每x秒运行一次的最佳方法。我必须使我的函数每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(获取匹配细节(h),10000)

不管这是否正确,这就是我试图说的“在10秒内再次运行此函数”,并继续这样做,直到每个方法都完成。它不起作用

如有必要,以下是我的
获取匹配详细信息
功能:

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);
            }
        });
}
提前谢谢你。

这正是我们的目的

因此,您可以使用类似以下内容来代替setTimeout:

var timer = setInterval(function() {

       get_match_details(h);

}, 1000);                              // for every second
如果要停止,请使用:

clearInterval(timer);
这正是我们的目的

因此,您可以使用类似以下内容来代替setTimeout:

var timer = setInterval(function() {

       get_match_details(h);

}, 1000);                              // for every second
如果要停止,请使用:

clearInterval(timer);

如果立即执行函数“获取匹配详细信息”,请将代码更改为

setTimeout( function() {
   get_match_details(h)
   }, 10000 );
代码中发生的事情是在所有播放器中循环,10秒后,将同时进行尽可能多的Ajax调用(
get\u match\u details

如果您想在每次ajax调用之间有10秒的间隔,您必须重构您的方法,如下所示:

  var l = [1,2,3,4,5];
  var c = l.length;
  var ix = -1;
  ajax = function( n ) {
    alert( 'do some ajax with ' + n )
  }
  call = function() {
    if( c > ++ix ) {
        ajax( l[ ix ] );
        setTimeout( function() { call(); }, 1000 );

    }
  }
  call();

如果立即执行函数“获取匹配详细信息”,请将代码更改为

setTimeout( function() {
   get_match_details(h)
   }, 10000 );
代码中发生的事情是在所有播放器中循环,10秒后,将同时进行尽可能多的Ajax调用(
get\u match\u details

如果您想在每次ajax调用之间有10秒的间隔,您必须重构您的方法,如下所示:

  var l = [1,2,3,4,5];
  var c = l.length;
  var ix = -1;
  ajax = function( n ) {
    alert( 'do some ajax with ' + n )
  }
  call = function() {
    if( c > ++ix ) {
        ajax( l[ ix ] );
        setTimeout( function() { call(); }, 1000 );

    }
  }
  call();