简体   繁体   中英

Interval gets blocked with two Ajax Calls

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!

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);
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM