终止JS运行有如下几种可能:

  1.终止函数的运行的方式有两种

    (1)在函数中使用return,则当遇到return时,函数终止执行,控制权继续向下运行

    (2)在函数中使用try-catch异常处理,需要结束时,使用throw抛出异常

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.终止动画特效的运行的方式是使用stop方法

    (1)stop([clearQueue], [gotoEnd]) :停止所有在指定元素上正在运行的动画

    (2)如果队列中有等待执行的动画(并且clearQueue没有设为true),将被马上执行

  3.终止表单提交的方式:在表单提交事件中使用return false;可以阻止表单提交

  4.终止定时函数执行的方式:使用window.clearInterval(定时器对象)或者window.clearTimeout(定时器对象);可以终止正在执行的定时器

相关文章:

  • 2021-11-15
  • 2022-12-23
  • 2021-12-09
  • 2022-01-12
  • 2022-12-23
  • 2021-10-03
  • 2021-10-22
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-17
  • 2022-12-23
  • 2021-07-02
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案