最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why am I getting the error clearImmediate is not defined? - Stack Overflow

programmeradmin0浏览0评论

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

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

Share Improve this question edited Jun 10, 2020 at 7:23 Liam 29.8k28 gold badges139 silver badges203 bronze badges asked Jun 9, 2020 at 11:03 Ewald BosEwald Bos 1,7801 gold badge21 silver badges36 bronze badges 2
  • window.clearImmediate should be window.clearTimeout – Kiran Shinde Commented Jun 9, 2020 at 11:05
  • 2 There is no clearImmediate - you need to use clearTimeout. Or alias it like you do with setImmediate. Not sure why you do that, though. – VLAZ Commented Jun 9, 2020 at 11:06
Add a ment  | 

2 Answers 2

Reset to default 3

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 inpatibilities between implementations and the behavior may change in the future.

and

This method is not expected to bee 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.

There is no function in Javascript named clearImmediate.

Instead to clear timeout there is clearTimeout.

So,

Use

window.clearTimeout(immediateId);

Instead of

window.clearImmediate(immediateId);
发布评论

评论列表(0)

  1. 暂无评论