-2

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.

Community
  • 1
  • 1
DanielM
  • 1,106
  • 3
  • 17
  • 27
  • What are you trying to do? You can implement infinite loop by `while (true)` – Roy Miloh Sep 23 '13 at 14:30
  • Asynchronous tasks, like timers, cannot complete until the execution thread is idle, which a `while (true)` will never let it be. – Jonathan Lonowski Sep 23 '13 at 14:32
  • I doubt the first example is working. [`continue`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/continue) is reserved. – Moritz Roessler Sep 23 '13 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 Sep 23 '13 at 14:33
  • [continue](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/continue), do not use it as a variable name. – epascarello Sep 23 '13 at 14:34
  • @jonhopkins It really isn't possible. – Roy Miloh Sep 23 '13 at 14:35
  • What are you trying to accomplish? – epascarello Sep 23 '13 at 14:35
  • If you give us an better idea of what you're trying to accomplish, we may be able to actually help you. – Moritz Roessler Sep 23 '13 at 14:44
  • Sorry, I have changed "continue" to "tocontinue" (I was using really a spanish variable name, so I had this mistake on translation, sorry). 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. – DanielM Sep 23 '13 at 14:47

4 Answers4

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 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/

Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
  • Thank you very much, Jonathan! There are so many options when you are not sure... I tried setTimeout with and without "function()" word, but not with setInterval. Sorry for my inexperience... Your solution is really elegant, and works very fine. – DanielM Sep 23 '13 at 15:12
3

setInterval() may be more useful here.

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

setInterval(updateLoop,500);
Daniel Jones
  • 188
  • 6
0
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;


 }

   }
-2
for(var I = 7; I >1; i+=3);
console.log(i)
digitalextremist
  • 5,952
  • 3
  • 43
  • 62
nova_n
  • 1