javascript - Stop calling of prompt box once 'Cancel' is clicked -


i have timer function shows user prompt box when remaining time goes below specific value.

code snippet of call

function countdown(duration, display, show) {         display.innerhtml = tohhmmss(duration, show);         timer = settimeout(function(){countdown(duration, display, show);}, 1000);         if(duration<1) {             cleartimeout(timer);             endsession();         }         duration--;     }      function tohhmmss(duration, show) {         if(duration<300 && show == 1) {             var newminutes = prompt("this session expire in 5 minutes. if want extend, enter minutes below");             if(newminutes == null) {                 /* stop showing alert box without affecting timer */             }             var elem = document.getelementbyid("timer");             elem.style.color = "red";         } else {             var elem = document.getelementbyid("timer");             elem.style.color = "#3bb9ff";         }         var sec_num = parseint(duration, 10);          var hours = math.floor(sec_num / 3600);         var minutes = math.floor((sec_num - (hours * 3600)) / 60);         var seconds = sec_num - (hours * 3600) - (minutes * 60);          if (hours < 10) {             hours = "0" + hours;         }         if (minutes < 10) {             minutes = "0" + minutes;         }         if (seconds < 10) {             seconds = "0" + seconds;         }         var time = hours + ':' + minutes + ':' + seconds;         return time;     } 

now, allows user enter value adds timer session run longer. issue comes when user clicks on cancel. check resides in settimeout call interval of 1 sec (the timer), alert getting called every second.

what do stop happening , @ same time not break out of function stop timer well

edit added complete code

it post complete code, here's quick jsfiddle showing how i'd go it: http://jsfiddle.net/eklingen/5zqecaap/1/

basically i'd go globals hold me:

var timer = document.getelementbyid('timer'); var tickinterval = setinterval(tick, 1000); var alertthreshold = 5; var remainingtime = 6; var prompted = false;  function tick() {       // need here } 

i trade out alerts , prompts modal dialogs keep timer running seeing how js likes freeze intervals , timeouts when 1 of boxes being displayed.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -