Reputation: 961
Here, my code reloads after every 10000 ms. Now, I need to add a button (id=button1) click function that will stop this page from reloading. But this following code is not working.
<script type="text/javascript">
var t=0;
if(t==0) {
window.setInterval(function () {
location.reload();
}, 10000);
}
$("#button1").click (function() {
t=1
alert(t);
});
});
</script>
Upvotes: 0
Views: 32
Reputation: 219106
var t=0;
if(t==0) {
Well, that's silly. t
will always equal 0
on that line because the line just before it set that value. So let's get rid of that unnecessary part and simplify the code:
window.setInterval(function () {
location.reload();
}, 10000);
$("#button1").click (function() {
// what to do here?
});
Now you're setting the interval and defining a click handler. In that click handler you want to clear the interval. You can do this with the value returned by setInterval
. For example:
var i = window.setInterval(function () {
location.reload();
}, 10000);
$("#button1").click(function() {
window.clearInterval(i);
});
Upvotes: 3