如何停止“setInterval” [英] How to stop "setInterval"

查看:189
本文介绍了如何停止“setInterval”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何停止并启动 setInterval

假设我有一个 textarea 。我想在焦点上停止 setInterval 并重新启动 setInterval on blur(使用jQuery)。

Suppose I have a textarea. I want to stop setInterval on focus and restart setInterval on blur (with jQuery).

推荐答案

启动时必须存储间隔的计时器ID ,稍后您将使用此值来停止它,使用 clearInterval 功能:

You have to store the timer id of the interval when you start it, you will use this value later to stop it, using the clearInterval function:

$(function () {
  var timerId = 0;

  $('textarea').focus(function () {
    timerId = setInterval(function () {
      // interval function body
    }, 1000);
  });

  $('textarea').blur(function () {
    clearInterval(timerId);
  });

});

这篇关于如何停止“setInterval”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆