Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.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-按正确顺序排列的几个间隔_Javascript_Setinterval - Fatal编程技术网

Javascript-按正确顺序排列的几个间隔

Javascript-按正确顺序排列的几个间隔,javascript,setinterval,Javascript,Setinterval,在我的代码中,我有三个间隔,应该一个接一个地随机执行 var times = Math.floor((Math.random() * 3) + 2); var turn = 0; var schemaInterval1 = setInterval(function(){ console.log('first'); turn++; if(turn == times) { time

在我的代码中,我有三个间隔,应该一个接一个地随机执行

    var times =  Math.floor((Math.random() * 3) + 2);
    var turn = 0;
    var schemaInterval1 = setInterval(function(){
        console.log('first');
        turn++;
        if(turn == times)
        {   
            times =  Math.floor((Math.random() * 3) + 2);
            turn = 0;
            clearInterval(schemaInterval1);
        }
    },1000);
    var schemaInterval2 = setInterval(function(){
        console.log('second');
        turn++;
        if(turn == times)
        {   
            times =  Math.floor((Math.random() * 3) + 2);
            turn = 0;
            clearInterval(schemaInterval2);
        }
    },1000);
        var schemaInterval3 = setInterval(function(){
        console.log('third');
        turn++;
        if(turn == times)
        {   
            times =  Math.floor((Math.random() * 3) + 2);
            turn = 0;
            clearInterval(schemaInterval3);
        }
    },1000);    
在间隔内每次执行代码后,我都会在turn值上加1。当转角和时间值相等时,我将重置这两个值。保留当前工作间隔并跳到另一个工作间隔。但不幸的是,它们的工作顺序不正确,在我的控制台中,我得到了这样的信息:

 first
 second
 third
 first
 second
 first
 (4)second

我如何重写我的代码,使这些间隔按正确的顺序排列,并且在第一次完成任务之前不允许其他人工作

您可以同时开始间歇。如果要串联运行它们,必须在清除前一个时启动一个。 此代码应满足您的要求:

var times=Math.floor((Math.random()*3)+2);
var-turn=0;
var区间数=1;
var间隔时间=1000;
var NumberOfInterval=3;
函数实例化Interval(){

如果(intervalNumber)将整个代码放在一个间隔内?