简体   繁体   English

为什么我的IF语句不执行? 为什么即使我插入clearStatement调用,间隔仍继续运行

[英]Why is my IF statement not executed?? And why does the interval continue to run even when I inserted a clearStatement call

The If statement is not being executed. If语句未执行。 I dont understand why? 我不明白为什么?

the only way I can clear the interval is if I print the command myself without the any if statements. 我可以清除间隔的唯一方法是,如果我自己打印命令而没有任何if语句。

var number = 0;

function addThis(){
  return number +=1;
}

var mySID = setInterval(addThis, 1000); 

if(number === 10){
  clearInterval(mySID);
  number;
}

It is running; 它正在运行; it's just not evaluating to true, so the "then" doesn't run. 它只是没有评估为真,因此“ then”不会运行。 Your if statement runs immediately after the call to setInterval , so number is still 0. It won't be 10 for another 10 seconds, but by then the if statement has long since finished. 您的if语句将在调用setInterval之后立即运行,因此number仍为0。在接下来的10秒钟内不会为10,但是到那时if语句已经结束了很长时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当readyState为4时,为什么我在脚本中调用的Ajax函数连续运行两次? - Why does the Ajax function I call in my script run twice in a row when readyState is 4? 当我在HTML中调用它时,为什么我的Javascript函数无法运行/工作? - Why does my Javascript function not run/work when I call it in HTML? 为什么onclick函数在onclick上运行,而在我手动调用时却不能运行? - Why does the onclick function run on onclick, but not when I call it manually? 为什么这个函数在ajax调用后执行时停止工作? - Why does this function stop working when executed after an ajax call? 为什么我的回叫功能无法运行? - Why does my call back function not run? 为什么继续? 声明冻结浏览器? - Why does continue; statement freeze browser? 当我不断单击时,为什么我的for循环不继续? - Why doesn't my for loop continue when I keep clicking? Ember js:在我的示例中,即使路由器中使用的功能尚未完成,为什么路由器中的逻辑仍然继续? - Ember js: In my example why does the logic in the router continue even though the function used in it has not completed? 为什么我的组件的这一部分会在间隔调用时重新呈现? - Why is this part of my component rerending on interval call? 当我在函数中使用if语句时,为什么我的javascript无法正常工作 - Why does my javascript not work when I use an if statement in my function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM