为什么我收到错误 clearImmediate is not defined?

Why am I getting the error clearImmediate is not defined?

我正在尝试制作一个在上传视频时停止的无限循环。该功能工作正常,但停止循环似乎不起作用。我得到的错误是:

clearImmediate is not defined

这是我要制作的循环:

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";

}

一旦 Azure 上传了它设置的视频 "videoIsUploaded = true" 这一切都正常并且 "if (videoIsUploaded..." 触发

Javascript 中没有名为 clearImmediate 的函数。

而不是清除超时有 clearTimeout

所以,

使用

window.clearTimeout(immediateId);

而不是

window.clearImmediate(immediateId);

Tl;博士

您应该只使用 setTimeoutclearTimeout


问题的症结似乎是您想使用 setImmediate。这是一种非标准方法,似乎并未在网络中广泛采用。见 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.

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).

它看起来像 async CPU bound operations 一样工作,但 webkit 和 Mozilla 不同意该实现,因此自 2011 年以来它一直在萎靡不振。

已在 node.js 中实施。几乎可以肯定,这在 node.js 应用程序中比在 Web 环境中更有意义,我假设这就是节点采用它的原因。我还假设您是从 node.js link?

中获得此代码的

我猜这就是你这样做的原因 window.setImmediate = window.setTimeout;。但这大多是毫无意义的。您应该只使用 setTimeoutclearTimeout.

顺便说一句 clearImmediate does exist 但它又是非标准的并且没有得到很好的支持。