Stop Javascript Function execution from another Function

后端 未结 3 1393
野的像风
野的像风 2021-02-14 04:41

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 r         


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-14 05:43

    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";
      }
    }
    
      
        My website
      
      
        
        
        
    This division can change colour

提交回复
热议问题