設定事件間隔和延遲,設定事件間隔延遲
<script type="text/javascript">var t=self.setInterval("clock()", 50);function clock(){ var time=new Date(); document.getElementById("clock").value=time;}function timeMsg(){ iSecond=Math.ceil((Math.random()*10)); var t=setTimeout("log()", iSecond*1000);}function log(){ $("#log").html("時間延遲:"+iSecond+"seconds!")}</script></head><body> <h2>jQuery設定時間間隔的方法</h2> <input type="text" id="clock" size="36"/> <button onclick="int=window.clearInterval(t)">stop interval</button> <h2>jQuery設定時間延遲的方法</h2> <form> <input type="button" value="Displayed timed alertbox!" onclick="timeMsg()"/> </form> <p>Click on the button above.An alert box will be displayed after 5 seconds.</p> <div id="log"></div></body>
setInterval(code,millisec[,”lang”]);
其中code參數表示要調用的函數或要執行的代碼串,millisec參數表示周期性執行或調用code之間以毫秒計的時間間隔
setTimeout(code,millisec[,”lang”])
其中code參數表示要調用的函數或要執行的代碼串,millisec參數表示在執行前需等待的毫秒數,code只執行一次,若要多次調用,必須使用setInterval()或讓code自身再次調用setTimeout()函數