0

I've got an Interval which runs a function every 3 Seconds.

intervalStepper = window.setInterval('intervalTick()','3000');

function intervalTick() {
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
    }
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            gotResult(xmlhttp.responseText);
        }
    }
    xmlhttp.open('GET','index.php?ajax=true',true);
    xmlhttp.send();
}
function gotResult(res) {
    alert(res);
}

Also, I have just another Ajax Call, which runs on button click.

if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
}
else {// code for IE6, IE5
    xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
}
xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
        agentDetailGotten(xmlhttp.responseText);
    }
}
xmlhttp.open('GET','index.php?anotherPage=true',true);
xmlhttp.send();

Now, if I run the second code just in time when the interval ticks and executes the first call, the calls actually run at about the same time. But then it seems like the interval dies somehow - he doies not tick anymore.

Is this a known problem or am I just not seeing something big...

Thanks for help!

Florian Müller
  • 7,448
  • 25
  • 78
  • 120
  • 3
    You're declaring **all** your variables as implicit globals. Make `xmlhttp` request local to your `intervalTick()` function with the `var` statement. This won't fix your issue, but it won't be helping it. – Matt Jun 01 '12 at 13:16
  • 2
    Furthermore, you should ***never*** pass strings to `setTimeout`/`setInterval`: use `window.setInterval(intervalTick,'3000');` instead. For more info see [here](http://stackoverflow.com/questions/6232574/is-it-bad-practice-to-pass-a-string-to-settimeout-if-yes-why) – Matt Jun 01 '12 at 13:16
  • 1
    Oh, and the timeout value for `setTimeout` and `setInterval` should be an integer, not a string, so use `window.setInterval(intervalTick, 3000);` – Matt Jun 01 '12 at 13:18

2 Answers2

0

Try to clear and set your interval again:

intervalStepper = window.setInterval('intervalTick()',3000);

function intervalTick() {

    window.clearInterval(intervalStepper);

    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
    }
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            gotResult(xmlhttp.responseText);
        }
    }
    xmlhttp.open('GET','index.php?ajax=true',true);
    xmlhttp.send();

    intervalStepper = window.setInterval('intervalTick()',3000);

}

function gotResult(res) {
    alert(res);
}
alexey28
  • 5,170
  • 1
  • 20
  • 25
0

I've solved it just now.

It seems this is something like a firefox bug (found on bugzilla.mozilla.org)

NS_ERROR_NOT_AVAILABLE

This one was not shown to me, but I've just found now. It appears when Firefox tries to execute two calls at the same time.

For more Information, I found a blog entry here

I solved it that if one call is running, the other one waits.

Florian Müller
  • 7,448
  • 25
  • 78
  • 120