我知道如何执行函数一次,这不是问题
首先,这是我的代码
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
,则是相同的过程。
但现在,如果第一个条件是true
和false
,我希望如果再次变成true
,那将是(1(
有什么方法可以读取清洁代码来做到这一点?因为,现在只有3个条件,但是可能有100个。
使用 clearInterval
停止执行您的功能,一个如果检查条件
var fid = setInterval(myFunction, 1000);
var cond = [false,false,false];
function myFunction()
{
console.log(cond.join());
// check conditions in some way here (e.g. every is true)
if(cond.every(x=>x)) {
clearInterval(fid);
console.log('Execute your code and stop');
}
// change conditions (example)
cond[2]=cond[1];
cond[1]=cond[0];
cond[0]=true;
}