Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.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 - Fatal编程技术网

Javascript 仅执行一次函数-取决于条件

Javascript 仅执行一次函数-取决于条件,javascript,Javascript,我知道如何执行一次函数,这不是问题所在 首先,这是我的代码 var executed1 = false; var executed2 = false; var executed3 = false; function myFunction() { if(-------------------) { //Verification - If the others conditions have ever been called if (ex

我知道如何执行一次函数,这不是问题所在

首先,这是我的代码

 var executed1 = false;
 var executed2 = false;
 var executed3 = false;

function myFunction()
{   
    if(-------------------)
    {
        //Verification - If the others conditions have ever been called
        if (executed2)
        {
            executed2 = false;
        }
        else if(executed3)
        {
            executed3 = false;
        }


        //Verification - If the condition have ever been called during this condition = true;
        if (!execution1) 
        {
            execution1 = true;
            //My code here ...
        }        
    }
    else if (-------------------)
    {

        if (executed1)
        {
            executed1 = false;
        }
        else if(executed3)
        {
            executed3 = false;
        }

        if (!execution2) 
        {
            execution2 = true;
            //My code here ...
        }                            
    }
    else if (-------------------)
    {
        //Same thing with execution3         
    }
}
setInterval("myFunction()", 10000);
我将以第一个条件为例

(1)如果条件为真,我想执行我的代码,但只是第一次:如您所见,我的函数每10秒执行一次。只要第一个条件为真,就不应该再附加任何条件

如果第一个条件变为
false
,第二个条件变为
true
,则是相同的过程

但是现在,如果第一个条件为
,我希望如果条件再次变为
,它将与(1)相同

有没有办法读一个更干净的代码来做到这一点?
因为,现在只有3个条件,但可能有100个。

使用
clearInterval
停止执行函数,如果要检查条件,则使用一个

var fid=setInterval(myFunction,1000);
var cond=[假,假,假];
函数myFunction()
{   
console.log(cond.join());
//在此处以某种方式检查条件(例如,every为true)
如果(cond.every(x=>x)){
清除间隔(fid);
log(“执行代码并停止”);
} 
//更改条件(示例)
cond[2]=cond[1];
cond[1]=cond[0];
cond[0]=真;
}
我向您介绍。。这个只需添加另一个变量“hasEverBeenTrue”并从(1)添加到if中的“| | haseverbeenvertrue”