-4

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

cweiske
31.4k15 gold badges150 silver badges206 bronze badges
asked Mar 29, 2017 at 9:07
3
  • 1
    You can stop a timer with clearTimeout. Though your variable should be accessible. (Google can be of great help.) Commented Mar 29, 2017 at 9:12
  • Can anyone provide feedback why I received down votes. Is it because it was considered too simple? Thanks Commented 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. Commented Mar 29, 2017 at 11:41

5 Answers 5

3

Move

var openTimer;

outside of the function to make it global.

answered Mar 29, 2017 at 9:09
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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

Comments

0

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

Comments

0
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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.