I have the following Javascript code
function scrollOpenTimed() {
var openTimer = setTimeout(function(){ scrollOpen(); }, 3000);
}
function a() {
var _ouibounce = ouibounce(document.getElementById('modal'), {
aggressive: true,
timer: 0,
callback: function() {
console.log('ouibounce fired!');
// cancel the openTimer
}
});
}
At the moment I have scrollOpen being triggered after 3 seconds.
I want to cancel the timer and stop scrollOpen function being triggered When the callback function in function a() / ouibounce() function is triggered.
Any ideas how to do this appreciated.
Thanks
-
1You can stop a timer with clearTimeout. Though your variable should be accessible. (Google can be of great help.)Ivar– Ivar2017年03月29日 09:12:07 +00:00Commented Mar 29, 2017 at 9:12
-
Can anyone provide feedback why I received down votes. Is it because it was considered too simple? Thanksejntaylor– ejntaylor2017年03月29日 09:26:41 +00:00Commented Mar 29, 2017 at 9:26
-
Probably because the answer can be found quitte easily. If you Google "Javascript stop timer", you can easily find how to do so. To do it from another question is a matter of the variable scope. That is basic Javascript (or even basic programming in general), and is not too hard to find either. (And both parts of this question are already answered on this site, so it would be considered duplicate as well.Ivar– Ivar2017年03月29日 11:41:50 +00:00Commented Mar 29, 2017 at 11:41
5 Answers 5
Move
var openTimer;
outside of the function to make it global.
answered Mar 29, 2017 at 9:09
Nina Scholz
388k26 gold badges367 silver badges417 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Following Nina Scholz's answer above, you should do:
var openTimer;
function scrollOpenTimed() {
openTimer = setTimeout(function(){ scrollOpen(); }, 3000);
}
function a() {
var _ouibounce = ouibounce(document.getElementById('modal'), {
aggressive: true,
timer: 0,
callback: function() {
console.log('ouibounce fired!');
// cancel the openTimer
clearTimeout(openTimer);
}
});
}
answered Mar 29, 2017 at 9:11
Nicolae Olariu
2,5652 gold badges20 silver badges31 bronze badges
Comments
You need to declare a publicly accessible variable to hold the instance of your timer.:
<script>
var openTimer = null;
function startTimer() {
if(openTimer == null)
{
openTimer = setTimeout(function(){console.log("I'll beprinted after 10 seconds") }, 10000);
}
}
function stopTimer(){
clearTimeout(openTimer);
openTimer = null;
}
</script>
<button onClick="startTimer();">Start Timer</button>
<br/>
<button onClick="stopTimer();">Stop Timer</button>
answered Mar 29, 2017 at 9:12
Shakti Phartiyal
6,3043 gold badges29 silver badges46 bronze badges
Comments
Try this:
var openTimer;
function scrollOpenTimed() {
openTimer = setTimeout(function() {
scrollOpen();
}, 3000);
}
function a() {
var _ouibounce = ouibounce(document.getElementById('modal'), {
aggressive: true,
timer: 0,
callback: function() {
console.log('ouibounce fired!');
clearTimeout(openTimer);
}
});
}
answered Mar 29, 2017 at 9:11
David Lampon
5003 silver badges13 bronze badges
Comments
var openTimer = null;
function scrollOpenTimed() {
openTimer = setTimeout(function(){ scrollOpen(); }, 3000);
}
function a() {
var _ouibounce = ouibounce(document.getElementById('modal'), {
aggressive: true,
timer: 0,
callback: function() {
console.log('ouibounce fired!');
if(openTimer ){
clearTimeout(openTimer );
}
}
});
}
answered Mar 29, 2017 at 9:12
Haddar Macdasi
3,5658 gold badges40 silver badges62 bronze badges
Comments
lang-js