停止从另一个函数执行 Javascript 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29149567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Stop Javascript Function execution from another Function
提问by Ramiz Ansari
Is there any way to stop the execution of a called function from another function?
有什么方法可以停止从另一个函数执行被调用的函数?
I have following code:-
我有以下代码:-
function MainFunction() { //a long code that runs for few time };
MainFuntion();
<button onclick="(Here I want some thing to stop MainFunction())">Stop the running script </button>
so basic idea is to return a function from another function
所以基本思想是从另一个函数返回一个函数
回答by Radu Balaban
JavaScript is normally single threaded - meaning that while a function is executed in the browser no other code can run at the same time - including event handlers such as onclick(they will be triggered only after the function is complete). So, in this case you cannot interrupt the execution of a function from code.
JavaScript 通常是单线程的——这意味着当一个函数在浏览器中执行时,没有其他代码可以同时运行——包括事件处理程序,例如onclick(它们只会在函数完成后被触发)。因此,在这种情况下,您不能从代码中中断函数的执行。
There are two workounds:
有两种解决方法:
The long running function could take breaks intentionally, allowing other code to execute.
//set this to true from an event handler to stop the execution var cancelled = false; function longRunningFunction() { if (cancelled) { return; } // do some work, but not all // save your progress to be able to resume when called again if (!done) { // release control, so that handlers can be called, and continue in 10ms setTimeout(longRunningFunction, 10); } }Use web workers. They allow running code in parallel, but have some restrictions and are not supported by all browsers.
长时间运行的函数可能会故意中断,从而允许其他代码执行。
//set this to true from an event handler to stop the execution var cancelled = false; function longRunningFunction() { if (cancelled) { return; } // do some work, but not all // save your progress to be able to resume when called again if (!done) { // release control, so that handlers can be called, and continue in 10ms setTimeout(longRunningFunction, 10); } }使用网络工作者。它们允许并行运行代码,但有一些限制,并非所有浏览器都支持。
回答by aravk33
You can pass an argument, like cancel, whenever you are calling the MainFunction. So, if you want the function to start, you pass in an arguement '0', and if you want it to stop, you can call the function again with an arguement which is not '0', like '1'. Here is a working example:
您可以在调用 MainFunction 时传递参数,例如取消。所以,如果你想让函数开始,你传入一个参数“0”,如果你想让它停止,你可以用一个不是“0”的参数再次调用函数,比如“1”。这是一个工作示例:
function MainFunction(cancel) {
var yourcode;
if (cancel == 0) {
yourCode = setInterval(function() {
//Put your code here, this is an example:
document.getElementById('div').style.color = "red";
}, 1);
}
if (cancel == 1) {
clearInterval(yourCode);
document.getElementById('div').style.color = "black";
}
}
<html>
<head>
<title>My website</title>
</head>
<body>
<button id="btn" onclick="MainFunction(0)">Start</button>
<button onclick="MainFunction(1)">Stop</button>
<div id="div">This division can change colour</div>
</body>
</html>
回答by Nirmal Dhara
I think you are trying to stop the execution of other function call from a function. if yes then you need to put the called function inside the condition so that you can control the execution.
我认为您正试图停止从一个函数执行其他函数调用。如果是,那么您需要将被调用的函数放在条件中,以便您可以控制执行。
if my understanding is not correct please elaborate what you want to do , mens why want to stop the execution of the function.
如果我的理解不正确,请详细说明您想做什么,男士为什么要停止执行该功能。

