Been playing with this in a fiddle for 4 hours now and cant find a solution…
HTML:
JavaScript
x
2
1
Real Time Data: <input type="checkbox" id="dataStream"/>
2
js:
JavaScript
1
11
11
1
var chartInt = null;
2
$("#dataStream").change(function() {
3
if(this.checked) {
4
var chartInt = setInterval(function() { alert('checked') }, 7000);
5
} else {
6
clearInterval(chartInt);
7
chartInt = null;
8
alert('unchecked');
9
}
10
});
11
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.net/5udtC/5966/
Thanks!
Advertisement
Answer
Don’t redefine the variable in the local scope
JavaScript
1
12
12
1
var chartInt = null;
2
$("#dataStream").change(function() {
3
if(this.checked) {
4
chartInt = setInterval(function() { // no "var" here
5
alert('checked')
6
}, 7000);
7
} else {
8
clearInterval(chartInt);
9
alert('unchecked');
10
}
11
});
12