Демонстрация методов setTimeout и setInterval
Main.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>main</title> </head> <body> <button id="SetInterval">SetInterval</button> <button id="SetTimeout">SetTimeout</button> <script src="EventHandler.js"></script> <hr> <div id="mydiv"> </div> </body> </html> |
EventHandler.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
/** * @author Stas */ var time; var ON; function CountDown(){ if (ON==true){ document.getElementById('mydiv').innerHTML=time+' seconds left'; if (time!==0){time=time-1;} else if (time===0) {document.getElementById('mydiv').innerHTML='time is over'; ON=false;} } }; function setIntervalDemo(){ time=prompt('What is delay in seconds?','2'); ON=true; setInterval(CountDown,1000); } function WriteToDiv(){ document.getElementById('mydiv').innerHTML= document.getElementById('mydiv').innerHTML+' Its Ready now! Its setTimeoutDemo'; } function setTimeoutDemo(){ document.getElementById('mydiv').innerHTML=''; time=prompt('What is delay in seconds?','2'); document.getElementById('mydiv').innerHTML='Wait... for '+time+' seconds...'; setTimeout(WriteToDiv,time*1000); //location.reload(); } document.getElementById('SetInterval').onclick=function(){setIntervalDemo();}; document.getElementById('SetTimeout').onclick=function(){setTimeoutDemo();}; |