How to stop "setInterval"

javascript, jquery, setinterval

Solution

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);
  });

});

Problem

How do I stop and start `setInterval`? Suppose I have a `textarea`. I want to stop `setInterval` on focus and restart `setInterval` on blur (with jQuery).

Original source

Related problems