clearInterval下面的代码会在setInterval内部执行吗?
Will the code below clearInterval be executed inside setInterval?
var myvar = setInterval (function(){
if (true) {
clearInterval(myvar);
}
alert('333');
},1000);
据我所知,alert('333');
将在执行上述代码时恰好出现一次(您可以在控制台中对其进行测试)。
我说得对吗?
当 if
中的语句为真时,排除执行此警报以便在 if
下不执行任何内容的最正确方法是什么。
What's the most proper way to exclude executing this alert so nothing is executed below if
使用return
.
var myfunc = setInterval (function(){
if (true) {
clearInterval(myfunc);
return;//now no further code will run
}
alert('333');
},1000);
As far as I see the alert('333'); will appear exactly one time upon execution of the code above (you can test it in the console). Am I right?
是的,你是对的。第一次调用回调会清除间隔,然后代码继续执行 alert
。不会有后续调用回调。
What's the most proper way to exclude executing this alert so nothing is executed below if, when the statement inside if is true
.
使用 else
或 return
。
else
:
var myvar = setInterval (function(){
if (true) {
clearInterval(myvar);
} else {
alert('333');
}
},1000);
return
:
var myvar = setInterval (function(){
if (true) {
clearInterval(myvar);
return;
}
alert('333');
},1000);
var myvar = setInterval (function(){
if (true) {
clearInterval(myvar);
}
alert('333');
},1000);
据我所知,alert('333');
将在执行上述代码时恰好出现一次(您可以在控制台中对其进行测试)。
我说得对吗?
当 if
中的语句为真时,排除执行此警报以便在 if
下不执行任何内容的最正确方法是什么。
What's the most proper way to exclude executing this alert so nothing is executed below if
使用return
.
var myfunc = setInterval (function(){
if (true) {
clearInterval(myfunc);
return;//now no further code will run
}
alert('333');
},1000);
As far as I see the alert('333'); will appear exactly one time upon execution of the code above (you can test it in the console). Am I right?
是的,你是对的。第一次调用回调会清除间隔,然后代码继续执行 alert
。不会有后续调用回调。
What's the most proper way to exclude executing this alert so nothing is executed below if, when the statement inside if is
true
.
使用 else
或 return
。
else
:
var myvar = setInterval (function(){
if (true) {
clearInterval(myvar);
} else {
alert('333');
}
},1000);
return
:
var myvar = setInterval (function(){
if (true) {
clearInterval(myvar);
return;
}
alert('333');
},1000);