How to disable/enable the javascript timer countdown?
<!-- CSS Code --> #txt { border:none; font-family:verdana; font-size:16pt; font-weight:bold; border-right-color:#FFFFFF } <!-- JavaScript file named: countDown.js --> var mins var secs; function cd() { mins = 1 * m("10"); // change minutes here secs = 0 + s(":01"); // change seconds here (always add an additional second to your total) redo(); } function m(obj) { for(var i = 0; i < obj.length; i++) { if(obj.substring(i, i + 1) == ":") break; } return(obj.substring(0, i)); } function s(obj) { for(var i = 0; i < obj.length; i++) { if(obj.substring(i, i + 1) == ":") break; } return(obj.substring(i + 1, obj.length)); } function dis(mins,secs) { var disp; if(mins <= 9) { disp = " 0"; } else { disp = " "; } disp += mins + ":"; if(secs <= 9) { disp += "0" + secs; } else { disp += secs; } return(disp); } function redo() { secs--; if(secs == -1) { secs = 59; mins--; } document.cd.disp.value = dis(mins,secs); // setup additional displays here. if((mins == 0) && (secs == 0)) { window.alert("Time is up. Press OK to continue."); // change timeout message as required // window.location = "yourpage.htm" // redirects to specified page once timer ends and ok button is pressed } else { cd = setTimeout("redo()",1000); } } function init() { cd(); } window.onload = init; <!-- HTML HEAD section --> <script type="text/javascript" src="countDown.js"></script> <!-- HTML BODY --> <form name="cd"> <input id="txt" readonly="true" type="text" value="10:00" border="0" name="disp"> </form>
Generally, the timer countdown, is required to stop user of doing something after a certain period of time. The above given example "javascript timer countdown" allows you to start and stop the timer after few seconds. Run the code to see how it function.
Thanks!
or you can write the timer enable/disable function as given below..
function timeroff() { var b = document.getElementById('Timer1'); if (!b.disabled) { b.disabled = true; } } function timeron() { var b = document.getElementById('Timer1'); if (b.disabled) { b.disabled = false; } }
Remember that the timer countdown function is useful for a program like online examination etc...
Ads