javascriptloopssettimeoutsetimmediateclearimmediate

Why am I getting the error clearImmediate is not defined?


i am trying to make an infinitive loop that stops when the video is uploaded. The function works fine, however to stop the loop does not seem to work. The error I get is:

clearImmediate is not defined

this is the loop i am trying to make:

window.setImmediate = window.setTimeout; //had to add this for it to start the loop

var videoIsUploaded = false;

var immediateId;

function loop() {

    console.log('still uploading');

    immediateId = setImmediate(loop);

    if (videoIsUploaded == true) {

        window.clearImmediate(immediateId);

        HideTheUpload();
    }
}

loop();

function HideTheUpload(){

   document.getElementById("AddVideo").style.display = "none";

}

Once Azure has uploaded the video it sets "videoIsUploaded = true" this all works fine and the "if (videoIsUploaded..." fires


Solution

  • Tl;Dr

    You should just use setTimeout and clearTimeout.


    The nub of your problem appears to be you wanting to use setImmediate. This is a non-standard method that does not appear to be widely adopted in the web. See the MDN site for this:

    This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.

    and

    This method is not expected to become standard, and is only implemented by recent builds of Internet Explorer and Node.js 0.10+. It meets resistance both from Gecko (Firefox) and Webkit (Google/Apple).

    It looks like this is designed to work like an async CPU bound operations but webkit and Mozilla disagreed with the implementation so it's been languishing since 2011.

    It has been implemented in node.js. This almost certainly makes a lot more sense in a node.js app than it does in a web context, I'm presuming this is why node adopted it. I'm also presuming that you got this code from a node.js link?

    I'm guessing that's why you're doing window.setImmediate = window.setTimeout;. But this is mostly pointless. You should just use setTimeout and clearTimeout.

    BTW clearImmediate does exist but it is again non-standard and not well supported.