-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
  • 30,033
  • 14
  • 133
  • 194
ejntaylor
  • 1,900
  • 1
  • 25
  • 43
  • 1
    You can stop a timer with [clearTimeout](http://stackoverflow.com/questions/11123237/how-to-stop-a-timer-function-from-running). Though your variable [should be accessible](http://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript). (Google can be of great help.) – Ivar Mar 29 '17 at 09:12
  • Can anyone provide feedback why I received down votes. Is it because it was considered too simple? Thanks – ejntaylor Mar 29 '17 at 09: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 Mar 29 '17 at 11:41

5 Answers5

3

Move

var openTimer;

outside of the function to make it global.

Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
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);
    }
  });
}
Nicolae Olariu
  • 2,487
  • 2
  • 18
  • 30
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>
Shakti Phartiyal
  • 6,156
  • 3
  • 25
  • 46
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);
    }
  });
}
David Lampon
  • 492
  • 3
  • 13
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 );
            }

        }
      });
}
Haddar Macdasi
  • 3,477
  • 8
  • 37
  • 59