RedSun RedSun - 1 month ago 10
HTML Question

I am setting a timer, but i am doing it in wrong way. So please give me better way to do this

I am setting a timer, but i am doing it in wrong way.
So please any one give me better way to do this.

this is code i have written ....
I know i have done it in wrong way, but its working.
i setting up timer for 40 sec.

<!DOCTYPE html>

<html><head><title>Online</title>
<script language="javascript">


function timedText()
{

var x=document.getElementById('txt');

var t40=setTimeout(function(){x.value="40 sec "},1000);
var t39=setTimeout(function(){x.value="39 sec "},2000);
var t38=setTimeout(function(){x.value="38 sec "},3000);
var t37=setTimeout(function(){x.value="37 sec "},4000);
var t36=setTimeout(function(){x.value="36 sec "},5000);
var t35=setTimeout(function(){x.value="35 sec "},6000);
var t34=setTimeout(function(){x.value="34 sec "},7000);
var t33=setTimeout(function(){x.value="33 sec "},8000);
var t32=setTimeout(function(){x.value="32 sec "},9000);
var t31=setTimeout(function(){x.value="31 sec "},10000);
var t30=setTimeout(function(){x.value="30 sec "},11000);

var t29=setTimeout(function(){x.value="29 sec "},12000);
var t28=setTimeout(function(){x.value="28 sec "},13000);
var t27=setTimeout(function(){x.value="27 sec "},14000);
var t26=setTimeout(function(){x.value="26 sec "},15000);
var t25=setTimeout(function(){x.value="25 sec "},16000);
var t24=setTimeout(function(){x.value="24 sec "},17000);
var t23=setTimeout(function(){x.value="23 sec "},18000);
var t22=setTimeout(function(){x.value="22 sec "},19000);
var t21=setTimeout(function(){x.value="21 sec "},20000);
var t20=setTimeout(function(){x.value="20 sec "},21000);

var t19=setTimeout(function(){x.value="19 sec "},22000);
var t18=setTimeout(function(){x.value="18 sec "},23000);
var t17=setTimeout(function(){x.value="17 sec "},24000);
var t16=setTimeout(function(){x.value="16 sec "},25000);
var t15=setTimeout(function(){x.value="15 sec "},26000);
var t14=setTimeout(function(){x.value="14 sec "},27000);
var t13=setTimeout(function(){x.value="13 sec "},28000);
var t12=setTimeout(function(){x.value="12 sec "},29000);
var t11=setTimeout(function(){x.value="11 sec "},30000);
var t10=setTimeout(function(){x.value="10 sec "},31000);

var t9=setTimeout(function(){x.value="9 sec "},32000);
var t8=setTimeout(function(){x.value="8 sec "},33000);
var t7=setTimeout(function(){x.value="7 sec "},34000);
var t6=setTimeout(function(){x.value="6 sec "},35000);
var t5=setTimeout(function(){x.value="5 sec "},36000);
var t4=setTimeout(function(){x.value="4 sec "},37000);
var t3=setTimeout(function(){x.value="3 sec "},38000);
var t2=setTimeout(function(){x.value="2 sec "},39000);
var t01=setTimeout(function(){x.value="1 sec "},40000);
var t0=setTimeout(function(){x.value="0 sec "},41000);

var wc=setTimeout(function()
{
document.quest.submit();
window.open('best2.php?username=$username');
window.close('best1.php?username=$username');
},42000);

}


</script>


</head>
<body onload="timedText()">

<br><br>
<form name="quest" method="POST" action="">

<p align='right'><b>Time left to answer this Question : </b>

<input type="text" id="txt" /></p>

<p><b>Question 1 : </b> Vanessa and Brett had been arguing about their perceived
proclivity to spend for hours together. What word describes the couple's predicament? <br></p>

<p><input type="radio" value="Wrong" name="ans" id="r1">Demarche </p>
<p><input type="radio" value="Correct" name="ans" id="r2">Impasse </p>

<p><input type="radio" value="Wrong" name="ans" id="r3">Mélange </p>
<p><input type="radio" value="Wrong" name="ans" id="r4">tête-à-tête </p>

<input type="submit" name="NEXT" value="Next" >
<br/>
<br/>
</form>
</body>
</html>

Answer

This will be a better way to set it:

var interval = setInterval(function() { /*doSomething*/ }, 1000)
setTimeout(function() { clearInterval(interval); }, 40000);