javascript - Can't stop setInterval with clearInterval - Stack Overflow

Been playing with this in a fiddle for 4 hours now and cant find a solution...HTML:Real Time Data: <

Been playing with this in a fiddle for 4 hours now and cant find a solution...

HTML:

Real Time Data: <input type="checkbox" id="dataStream"/>

js:

var chartInt = null;
$("#dataStream").change(function() {
    if(this.checked) {
        var chartInt = setInterval(function() { alert('checked') }, 7000);
    } else {
        clearInterval(chartInt);
        chartInt = null;
        alert('unchecked');
    }
});

Note: because clearInterval is not working you need to click on "run" in the jsfiddle to get it to stop after clicking the checkbox, you have 7 seconds between alerts...

Here is a link to the jsfiddle: /

Thanks!

Been playing with this in a fiddle for 4 hours now and cant find a solution...

HTML:

Real Time Data: <input type="checkbox" id="dataStream"/>

js:

var chartInt = null;
$("#dataStream").change(function() {
    if(this.checked) {
        var chartInt = setInterval(function() { alert('checked') }, 7000);
    } else {
        clearInterval(chartInt);
        chartInt = null;
        alert('unchecked');
    }
});

Note: because clearInterval is not working you need to click on "run" in the jsfiddle to get it to stop after clicking the checkbox, you have 7 seconds between alerts...

Here is a link to the jsfiddle: http://jsfiddle/5udtC/5966/

Thanks!

Share Improve this question edited Feb 16, 2016 at 8:45 Puck 2,1204 gold badges21 silver badges33 bronze badges asked Mar 14, 2014 at 21:32 Ian TIan T 531 silver badge4 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

Don't redefine the variable in the local scope

var chartInt = null;
$("#dataStream").change(function() {
    if(this.checked) {
        chartInt = setInterval(function() {  // no "var" here
            alert('checked') 
        }, 7000);
    } else {
        clearInterval(chartInt);
        alert('unchecked');
    }
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744398143a4572251.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信