在 JavaScript 上尝试无限循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18961878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 13:51:50  来源:igfitidea点击:

Trying a infinite loop on JavaScript

javascriptinfinite-loop

提问by DanielM

I am trying to do a infinite loop, but it only works if I include an 'alert' on it. My code looks like this:

我正在尝试进行无限循环,但只有在其上包含“警报”时才有效。我的代码如下所示:

while( tocontinue ){
  // Some code
  alert('Accept to continue');
}

On this way, the user has to click to hide the alerts (for example, on Chrome), and then the loop continues correctly by itself. I need to implement this without any alert. I also tried this:

通过这种方式,用户必须单击以隐藏警报(例如,在 Chrome 上),然后循环自行正确继续。我需要在没有任何警报的情况下实现这一点。我也试过这个:

while( tocontinue ){
  // Some code
  tocontinue = false;
  setTimeout(function(){tocontinue=true},500);
}

And with "window.setTimeout" too, and without the word "function(){}", but it doesn't work. I tried everything: some implementations on JavaScript of a sleep() function, calling the function each Xtime with setInterval, answers 1 and 3 on this post... :/

还有“window.setTimeout”,没有“function(){}”这个词,但它不起作用。我尝试了一切:sleep() 函数在 JavaScript 上的一些实现,每X次使用 setInterval调用该函数,在这篇文章中回答 1 和 3 ......:/

Thank you very much for your time.

非常感谢您的宝贵时间。

回答by Jonathan Lonowski

I'm trying to implement a genetic algorithm, and I want to stop it when I decide (with a button that puts the global variable "tocontinue" to false). Meanwhile, I want a infinite loop.

我正在尝试实现一个遗传算法,我想在我决定时停止它(使用一个将全局变量“tocontinue”设置为 false 的按钮)。同时,我想要一个无限循环。

Well, you won't be able to combine a true infinite loop with user interaction as they'll both be dependent on the same thread being able to work on them exclusively. But, you can get close with a near-instant interval.

好吧,您将无法将真正的无限循环与用户交互结合起来,因为它们都依赖于能够专门处理它们的同一个线程。但是,您可以以近乎即时的间隔接近。

var interval = setInterval(function () {
    // some code
}, 10);

Possibly grouping a few iterations together for each round:

可能将每一轮的几次迭代组合在一起:

var interval = setInterval(function () {
    var limit = 5;
    while (limit--) {
        // some code
    }
}, 10);

But, the interval will keep the iteration going as quickly as possible while still giving some idle time for user interactions, like clicking a particular button to clear the interval.

但是,间隔将保持迭代尽可能快地进行,同时仍为用户交互留出一些空闲时间,例如单击特定按钮以清除间隔

document.getElementById('stopButton').addEventListener('click', function () {
    clearInterval(interval);
}, false);

Example: http://jsfiddle.net/coiscir/xZBTF/

示例:http: //jsfiddle.net/coiscir/xZBTF/

回答by Daniel Jones

setInterval()may be more useful here.

setInterval()在这里可能更有用。

function updateLoop() {
   //All the code goes here 
}

setInterval(updateLoop,500);

回答by Shiv Singh

var reader = new XMLHttpRequest() || new ActiveXObject('MSXML2.XMLHTTP');

function loadFile() {

    reader.open('get', 'ccc.txt', true); 
    reader.onreadystatechange = displayContents;
    reader.send(null);
}

function displayContents() {

if(reader.readyState==4) {

var el = document.getElementById('main');

el.innerHTML = reader.responseText;

var data =  el.innerHTML;


 }

   }

回答by nova_n

for(var I = 7; I >1; i+=3);
console.log(i)