PDA

View Full Version : JavaScript Timeout


macwiz
07-18-2008, 01:11 AM
This script will execute other functions when time runs out. I found it very useful when timing Radio Button quizzes and what not that are scored with JavaScript. Simply add this to your page:

<body onload="begintimer();">



There are comments in the appropriate places.


<script language="javascript">


//enter limit in "minutes:seconds" Minutes should range from 0 to inifinity. Seconds should range from 0 to 59
var limit="30:00"
if (document.images){
var parselimit=limit.split(":")
parselimit=parselimit[0]*60+parselimit[1]*1
}
function begintimer(){
if (!document.images)
return
if (parselimit==1)
{
//ACTIONS HERE!!!!
}

else{
parselimit-=1
curmin=Math.floor(parselimit/60)
cursec=parselimit%60
if (curmin!=0)
curtime=curmin+" minutes and "+cursec+" seconds left"
else
curtime=cursec+" seconds left"
window.status=curtime
setTimeout("begintimer()",1000)
}
}
</script>


Tell me what you think please. Thanks.

rangana
07-18-2008, 02:32 AM
It's good. Just one thing. language is a deprecated attribute, use type instead.

You might also want to separate JS from our markup by:

window.onload=begintimer;


In replacement of the onload attribute.

Anyway, that was just my thoughts, code-wise, great! :)

macwiz
07-18-2008, 03:02 AM
Thanks!

I don't like to use window.onload as some people see it happen, but have no idea where the script comes from. I have done that several times. This way, you see the function being called.