timer error

Results 1 to 2 of 2

Thread: timer error

  1. #1
    Join Date
    Dec 1969

    Default timer error

    All,<BR>here is some code that i use for a users time out session. I want to have a function the will reset the time. I created a function that resets the time BUT it counts down 2x as fast and increases with every call to the rest function. I cant figure out why this is doing this. any ideas?<BR><BR><BR>Chopps<BR><BR>&#060;SCRIPT LANGUAGE="JavaScript"&#062;<BR><BR>&#060;!-- Begin<BR>// Take user here after session timed out<BR>timedouturl = "Default.asp?s=end";<BR><BR>function Minutes(data) {<BR>for (var i = 0; i &#060; data.length; i++)<BR>if (data.substring(i, i + 1) == ":")<BR>break;<BR>return (data.substring(0, i));<BR>}<BR><BR>function Seconds(data) {<BR>for (var i = 0; i &#060; data.length; i++)<BR>if (data.substring(i, i + 1) == ":")<BR>break;<BR>return (data.substring(i + 1, data.length));<BR>}<BR><BR>function Display(min, sec) {<BR>var disp;<BR>if (min &#060;= 9) disp = " 0";<BR>else disp = " ";<BR>disp += min + ":";<BR>if (sec &#060;= 9) disp += "0" + sec;<BR>else disp += sec; <BR>return (disp);<BR>}<BR><BR>function Down() { <BR>sec--;<BR>//alert(sec); <BR>if (sec == -1) { sec = 59; min--; }<BR>document.timerform.clock.value = Display(min, sec);<BR>window.status = "Session will time out in: " + Display(min, sec);<BR>if (min == 0 && sec == 0) {<BR>alert("Your session has timed out due to inactivity. Please login again.");<BR>window.location.href = timedouturl;<BR>}<BR>else down = setTimeout("Down()", 1000);<BR>}<BR><BR>function timeIt() {<BR>min = 1 * Minutes(document.timerform.clock.value);<BR>sec = 0 + Seconds(document.timerform.clock.value);<BR><BR>Do wn();<BR>}<BR><BR>function timeItReset() {<BR>min = 1 * Minutes(document.timerform.resetclock.value);<BR>s ec = 0 + Seconds(document.timerform.resetclock.value);<BR>< BR><BR>Down();<BR>}<BR><BR>function ResetTimer() {<BR>setTimeout("Down()", 500);<BR><BR>timeItReset();<BR><BR>}<BR>// End --&#062;<BR>&#060;/script&#062;<BR><BR>&#060;BODY OnLoad="timeIt()"&#062;<BR><BR>&#060;form name="timerform"&#062;<BR>&#060;input type="hidden" name="clock" size="7" value="10:00"&#062;<BR>&#060;input type="hidden" name="resetclock" size="7" value="10:00"&#062;<BR>&#060;/form&#062;<BR><BR>

  2. #2
    Join Date
    Dec 1969

    Default No you did not...

    you did *NOT* "reset" the original timer...you just added *another* one! So now both timers are running and, indeed, the clock runs twice as fast.<BR><BR>You MUST *NOT* CALL Down() from *either* of the reset functions! The setTimeout that Down() itself has started *will* continue to fire, on time.<BR><BR>Each time you call Down() you start off *another* timer. <BR><BR>You *can* kill the old timers (read the JS docs), but you don&#039;t really need to in this code. Just don&#039;t call Down() more than one time per page (except when it calls itself with the timeout) and it should work.<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts