在setTimeout JavaScript函数中停止循环条件 [英] Stop the loop condition in setTimeout javascript function

查看:110
本文介绍了在setTimeout JavaScript函数中停止循环条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

海,

Hai,

var t;

function start()
{
   t=setTimeout(''call'',1000);
}

function call()
{
while(true)
{
...
...
}
}



有些情况下,我应该停止函数调用循环.所以我正在使用clearTimeout(t).但它不起作用. while循环仍在继续.因此,如何停止while循环.



Some condition i should stop the function call loop. So i am using clearTimeout(t). But its not working. The while loop is still continue. So how to stop the while loop.

推荐答案

您的while循环将始终继续,因为您使用的是TRUE. clearTimeout没什么区别,因为该函数已经被调用并且while循环正在处理.您需要使用其他比较;像这样的东西.

Your while loop will always continue because you are using TRUE. clearTimeout makes no difference because the function has already been called and the while loop is processing. You need to use some other comparison; something like this.

var continue = true;
while(continue)
{
  if(stop loop)
     continue = false;
}


这篇关于在setTimeout JavaScript函数中停止循环条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆