简体   繁体   English

我可以通过全局变量停止 JavaScript function 执行吗?

[英]Can I stop JavaScript function execution via global variables?

So I read through this a bit, and it looks like JavaScript functions can't be explicitly killed off mid-run.所以我通读一下,看起来 JavaScript 函数不能在运行中被明确地杀死。 What if I have a global variable, window.currentlyProcessing that manages this.如果我有一个全局变量window.currentlyProcessing来管理这个怎么办。

function contentsChanged()
{
    if( window.curentlyProcessing == true )
    {
        return;
    }
    window.curentlyProcessing = true;
    // DO STUFF
    window.curentlyProcessing = false;
}

Since contentsChanged gets called a lot, will this effectively stop it from running over itself?由于contentsChanged被调用了很多,这会有效地阻止它自己运行吗?

Javascript is single threaded - one function is executed at a time, the function will never "run over itself" to begin with. Javascript 是单线程的 - 一次执行一个 function,function 一开始就永远不会“超越自身”。 contentsChanged will get called, will execute until the end, then any other stuff will happen. contentsChanged将被调用,将执行到最后,然后会发生任何其他事情。

That would prevent the logic from running if it's already running.如果它已经在运行,这将阻止逻辑运行。 If that's what you need to do, it should work.如果这是你需要做的,它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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