How to stop a timer function from running?

前端 未结 1 722
既然无缘
既然无缘 2020-12-19 10:49

I\'m a bit new to js and have been trying to figure out how to stop this function from running when I click on a button. I tried using clearInterval but I am not sure I am d

相关标签:
1条回答
  • 2020-12-19 10:51

    You need to give JavaScript a reference to the interval:

    var t = setTimeout(function() {
        timer(counter + 1);
    }, 3000);
    

    Then you can clear it like so:

    $("#stop").click(function () {
       clearTimeout(t);
    });
    
    0 讨论(0)
提交回复
热议问题