3

I am trying to timeout a function in case it has an infinite loop. But the below does not work. Any idea why and how to fix it?

setTimeout(clearValues,1000);

function clearValues(){
    i=0;
    alert("hi "+i);
}

i=19
function infin(){
    while(i>0){
        console.log(i);
        i++;
    }
    alert("Done");
}

infin();

In the below case, I get the alert displayed ( a bit later than expected ) and the console statements continue printing even after the alert. That means setTimeout did not wait for the loop to end in this case. Any explanation for this?

setTimeout(clearValues,500);

function clearValues(){
    alert("clear");
}


function infin(){
for(i=0;i<10000;){
    i=i+0.3;
    console.log(i);
}
}

infin();
3
  • On which action are u calling these functions?? Commented Jun 29, 2015 at 6:09
  • No specific action. This just runs as a script. Commented Jun 29, 2015 at 6:10
  • the second example does work only after the loop finished. Commented Jun 29, 2015 at 6:40

3 Answers 3

3

setTimeout works asynchronously, means it will run after 1000ms and the previous event loop is completed. Since the while loop will never be completed, the callback will never be called.

Add a condition to the loop if you want to exit it.

Sign up to request clarification or add additional context in comments.

1 Comment

It probably has to do with the implementation of console.log. if you'll test it in different browser you'll probably get different results.
1

Another solution might be to use interval:

var code = function(){ 
              console.log('tick'); 
           };
var clock = setInterval(code, 200);

When you don't need it anymore:

clearInterval(clock);

4 Comments

OP is using setTimeout and not setInterval
Sorry but what OP stands for?
It is the person who asks the question
@JeanF. OP - Original Poster
0

It works, when you made the infin call with a slightly different change.

var i = 0;
setTimeout(clearValues, 1000);
var interval = setInterval(infin, 0);

function clearValues() {
    out("clear");
    clearInterval(interval);
}

function infin() {
    if (i < 10000) {                    // if you be shure that you
        i++;
        out(i);
    } else {                            // never reach the limit,
         clearInterval(interval);       // you can remove the four
    }                                   // commented lines
}

function out(s, pre) {
    var descriptionNode = document.createElement('div');
    if (pre) {
        var preNode = document.createElement('pre');
        preNode.innerHTML = s + '<br>';
        descriptionNode.appendChild(preNode);
    } else {
        descriptionNode.innerHTML = s + '<br>';
    }
    document.getElementById('out').appendChild(descriptionNode);
}
<div id="out"></div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.