Method . setTimeout () actually returns a reference to the timer it creates. This link can be used in .clearTimeout to stop the timer before executing it.
Here is an example of how to use it:
var timer; if($('.main-popup2').is(":visible")){ // create the timer and save its reference timer = setTimeout(function() { $('.main-popup2').fadeOut('fast'); }, 20000); } // when clicking somewhere on the page, stop the timer $(document).click(function() { clearTimeout(timer); }):
source share