Stop Javascript Function execution from another Function

不打扰是莪最后的温柔 提交于 2020-01-12 07:32:06

问题


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


回答1:


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.

There are two workounds:

  1. 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);
      }
    }
    
  2. Use web workers. They allow running code in parallel, but have some restrictions and are not supported by all browsers.




回答2:


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:

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>



回答3:


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.




回答4:


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>


来源:https://stackoverflow.com/questions/29149567/stop-javascript-function-execution-from-another-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!