Trying a infinite loop on JavaScript - Stack Overflow

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

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:

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 X time with setInterval, answers 1 and 3 on this post... :/

Thank you very much for your time.

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:

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 X time with setInterval, answers 1 and 3 on this post... :/

Thank you very much for your time.

Share Improve this question edited May 23, 2017 at 12:18 CommunityBot 11 silver badge asked Sep 23, 2013 at 14:27 DanielMDanielM 1,1953 gold badges19 silver badges29 bronze badges 9
  • What are you trying to do? You can implement infinite loop by while (true) – Roy Miloh Commented Sep 23, 2013 at 14:30
  • Asynchronous tasks, like timers, cannot plete until the execution thread is idle, which a while (true) will never let it be. – Jonathan Lonowski Commented Sep 23, 2013 at 14:32
  • I doubt the first example is working. continue is reserved. – Moritz Roessler Commented Sep 23, 2013 at 14:32
  • @RoyMiloh I think continue here is actually the name of a boolean variable OP is using.. Though I didn't think that was possible.. – jonhopkins Commented Sep 23, 2013 at 14:33
  • continue, do not use it as a variable name. – epascarello Commented Sep 23, 2013 at 14:34
 |  Show 4 more ments

4 Answers 4

Reset to default 4

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.

Well, you won't be able to bine 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/coiscir/xZBTF/

setInterval() may be more useful here.

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

setInterval(updateLoop,500);
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;


 }

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744351925a4570029.html

相关推荐

  • Trying a infinite loop on JavaScript - Stack Overflow

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

    7天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信