从循环启动功能,但仅一次

时间:2018-10-09 17:13:11

标签: javascript php ajax loops

好的,我正在为在线4人游戏制作大厅功能。一切就绪,当所有4位玩家都加入大厅时,我想开始倒数10秒,以便玩家单击客户端的接受比赛。(灵感来自csgo) 我使用ajax从php文件中调用和刷新大厅数据,因此实时统计信息显示了谁在大厅。 这是一个循环,并在这里出现问题。我希望在所有4位玩家都加入时调用倒计时功能,并在某个玩家离开时立即停止计时,但是随着父功能进入循环(使用javascript中的设置间隔),它还会反复调用倒计时功能。 尝试过此方法,但无济于事。

function loadlobbymodule(x){
var spl = x.split(",");
var lid = spl[1];
    $.ajax({
    url: 'inc/fn/lobbyload.php?lid='+ lid,
    success: function(data) {
        var spl = data.split(",");
        var p1 = spl[0];
        var p2 = spl[1];
        var p3 = spl[2];
        var p4 = spl[3];
        var pl = spl[4];
        var pleft = 4 - pl;
        var p1_i = spl[5];
        var p2_i = spl[6];
        var p3_i = spl[7];
        var p4_i = spl[8];
        if(p1!=''){
            $('#slot1').html('<img src="../../img/' + p1_i +'" width="100px" height="100px"><Br><br>' + p1);
        } else{
            $('#slot1').html('');
        }
        if(p2!=''){
            $('#slot2').html('<img src="../../img/' + p2_i +'" width="100px" height="100px"><Br><br>' + p2);
        }else{
            $('#slot2').html('');
        }
        if(p3!=''){
            $('#slot3').html('<img src="../../img/' + p3_i +'" width="100px" height="100px"><Br><br>' + p3);
        }else{
            $('#slot3').html('');
        }
        if(p4!=''){
            $('#slot4').html('<img src="../../img/' + p4_i +'" width="100px" height="100px"><br><br>' + p4);
        }else{
            $('#slot4').html('');
        }
        $('#slotl').html(pleft + ' players left to join.');
        if(pl == 4){
            var accept = 1;
        } else{
            var accept = 0;
        }
        //alert(accept + ' sent');
        accepto(accept);
      }

});
}
function accepto(x){
    //alert(x + ' reached');
    if (x == 1){
        if (!timeleft){
            timeleft = 10;
            var downloadTimer = setInterval(function(){
            timeleft--;
            $('.h3').html('All players joined. Accept in ' + timeleft + ' seconds');
            if(timeleft <= 0)
                clearInterval(downloadTimer);
            },1000);
        }
    } else {
        $('.h3').html('Waiting for players to join');
    }
}setInterval(function(){loadlobbymodule(loll)}, 500);

1 个答案:

答案 0 :(得分:1)

将计时器设为全局,并继续检查活跃的玩家

window.myGlobalTimer = yourTimer()
listening_PlayerThatLeave : ()=>{
  clearTimeout(myGlobalTimer)
}

listening_AllPlayersIn : ()=>{
  window.myGlobalTimer
}

/* dont keep making a new timeOut everytime , instead use the global var*/
function accepto(x){
    //alert(x + ' reached');
    if (x == 1){
        if (!timeleft){
            timeleft = 10;
//here
            var downloadTimer = setInterval(function(){
            timeleft--;
            $('.h3').html('All players joined. Accept in ' + timeleft + ' seconds');
            if(timeleft <= 0)
                clearInterval(downloadTimer);
            },1000);
        }
    } else {
        $('.h3').html('Waiting for players to join');
    }
}