js implementation method to terminate execution

There are several possibilities to terminate JS running:

  1. There are two ways to terminate the operation of the function

    (1) Use return in the function, then when return is encountered, the function terminates execution, and the control continues to run down

    (2) Use try-catch exception handling in the function, and use throw to throw an exception when it needs to end

function getRectArea(width, height) {
  if (isNaN(width) || isNaN(height)) {
    throw "Parameter is not a number!";
  }
}

try {
  getRectArea(3, 'A');
}
catch(e) {
  console.log(e);
  // expected output: "Parameter is not a number!"
}

  2. The way to terminate the running of the animation effect is to use the stop method

    (1) stop([clearQueue], [gotoEnd]) : Stop all animations running on the specified element

    (2) If there is an animation waiting to be executed in the queue (and clearQueue is not set to true), it will be executed immediately

  3. Terminate form submission: use return false in the form submission event; form submission can be prevented

  4. The way to terminate the execution of the timing function: use window.clearInterval (timer object) or window.clearTimeout (timer object); you can terminate the executing timer

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324860333&siteId=291194637