質問

I have 1 button that toggles on and off: button on -> onrepeat = true button off -> onrepeat = false

If onrepeat is true, the setinterval to kicks off which it does just fine. If the onrepeat is false, i want it to clear the interval.

My clear interval does not stop the the interval, it just keeps going:

 function checkRepeat() {

      var int = setInterval(repeat, 6000);

      if (onrepeat === false) {
          $("#repeat_toggle").attr("class", "repeat_button");

          repeat();
          int;

          onrepeat = true;

      }

      else {

          clearInterval(int);
          $("#repeat_toggle").attr("class", "repeat_off_button");
          onrepeat = false;

       }


  }

Any idea what's wrong?

役に立ちましたか?

解決

int is scoped to the function. It gets reset each time you call checkRepeat

You need to scope it to a level above the function.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top