Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript timer help needed
    text
    copied!<p>I have the following ajax page. The form should be submitted and the timer should be restart counting down when the timer expires or button is clicked.<br> For some reason the timer is not reset and goes minus. Can someone tell me how to fix this? </p> <p>(The code is standalone, you can copy/paste ans test it, then run it on server) </p> <p><strong><code>test.htm:</code></strong></p> <pre><code> &lt;html> &lt;head> &lt;script type="text/javascript" src="http://mootools.net/download/get/mootools-core-1.3-full-compat.js">&lt;/script> &lt;/head> &lt;body> &lt;div id="container"> &lt;form id="form" name="form" action="test.htm" method="post"> Remaining time: &lt;input type="text" name="clock" size="4">seconds &lt;input type="submit" value="submit" ID="questionSubmit"> &lt;/form> &lt;script type="text/javascript"> var timeLeft = 10; function startClock() { timeLeft--; document.form.clock.value = timeLeft; if(timeLeft == 0) { sendForm(); } else { setTimeout("startClock()", 1000); } } setTimeout("startClock()", 1000); &lt;/script> &lt;script type="text/javascript"> function handleSubmit(e) { e = new Event(e).stop(); sendForm(); } function sendForm() { $('form').set('send', {onComplete: function(response) { $('container').set('html', response); $('questionSubmit').addEventListener('click', handleSubmit, false); setTimeout("startClock()", 1000); }}); //Send the form. $('form').send(); } $('questionSubmit').addEventListener('click', handleSubmit, false); &lt;/script> &lt;/div> &lt;/body> &lt;/html> </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload