javascript計時器事件使用詳解_基礎知識

來源:互聯網
上載者:User

在 JavaScritp 中使用計時事件是很容易的,兩個關鍵方法是:

setTimeout()
未來的某時執行代碼

clearTimeout()
取消setTimeout()
setTimeout()
文法

複製代碼 代碼如下:

var t=setTimeout("javascript語句",毫秒)


setTimeout() 方法會返回某個值。在上面的語句中,值被儲存在名為 t 的變數中。假如你希望取消這個 setTimeout(),你可以使用這個變數名來指定它。
setTimeout() 的第一個參數是含有 JavaScript 語句的字串。這個語句可能諸如 "alert('5 seconds!')",或者對函數的調用,諸如 alertMsg()"。

第二個參數指示從當前起多少毫秒後執行第一個參數。

提示:1000 毫秒等於一秒。

當下面這個例子中的按鈕被點擊時,一個提示框會在5秒中後彈出。

複製代碼 代碼如下:

<html>
<head>
<script type="text/javascript">
function timedMsg()
 {
 var t=setTimeout("alert('5 seconds!')",5000)
 }
</script>
</head>

<body>
<form>
<input type="button" value="Display timed alertbox!" onClick="timedMsg()">
</form>
</body>
</html>

執行個體 - 無窮迴圈

要建立一個運行於無窮迴圈中的計時器,我們需要編寫一個函數來調用其自身。在下面的例子中,當按鈕被點擊後,輸入欄位便從 0 開始計數。

複製代碼 代碼如下:

<html>

<head>
<script type="text/javascript">
var c=0
var t
function timedCount()
 {
 document.getElementById('txt').value=c
 c=c+1
 t=setTimeout("timedCount()",1000)
 }
</script>
</head>

<body>
<form>
<input type="button" value="Start count!" onClick="timedCount()">
<input type="text" id="txt">
</form>
</body>

</html>



clearTimeout()

文法

複製代碼 代碼如下:

clearTimeout(setTimeout_variable)
 

執行個體

下面的例子和上面的無窮迴圈的例子相似。唯一的不同是,現在我們添加了一個 "Stop Count!" 按鈕來停止這個計數器:

複製代碼 代碼如下:

<html>

<head>
<script type="text/javascript">
var c=0
var t

function timedCount()
 {
 document.getElementById('txt').value=c
 c=c+1
 t=setTimeout("timedCount()",1000)
 }

function stopCount()
 {
 clearTimeout(t)
 }
</script>
</head>

<body>
<form>
<input type="button" value="Start count!" onClick="timedCount()">
<input type="text" id="txt">
<input type="button" value="Stop count!" onClick="stopCount()">
</form>
</body>
</html>



另外兩個重要的方法:
複製代碼 代碼如下:

setInterval()
setInterval() - executes a function, over and over again, at specified time intervals

作用是:迴圈執行一個方法,在規定的間隔時間內

文法:

複製代碼 代碼如下:

window.setInterval("javascript function",milliseconds);

說明:第一個參數必須是一個函數,第二個參數是執行函數的間隔時間.

執行個體:

複製代碼 代碼如下:

<html>
<script type="text/javascript">
setInterval(function() {alert("hello")},500);
</script>
</html>

說明:上面例子,執行效果是說每隔500ms就alert("hello");

再來一個時鐘:

複製代碼 代碼如下:

<html>
<body>
<p id="demo" ></p>
<script type="text/javascript">
setInterval(function(){ myTimer()},1000);
        function  myTimer(){
                var d = new Date();
                var t=d.toLocaleTimeString();
                document.getElementById('demo').innerHTML=t;
        }
</script>
</body>
</html>    

如何停止,setInterval()方法??

複製代碼 代碼如下:

window.clearInterval()

文法:
複製代碼 代碼如下:

window.clearInterval(intervalVariable)


複製代碼 代碼如下:

The window.clearInterval() method can be written without the window prefix.

To be able to use the clearInterval() method, you must use a global variable when creating the interval method:

myVar=setInterval("javascript function",milliseconds);
Then you will be able to stop the execution by calling the clearInterval() method.

執行個體:

複製代碼 代碼如下:

<html>
<body>
<p id="demo" ></p>
<p id="demo2" onclick="stop()">stop</p>
<script type="text/javascript">
var temp=setInterval(function(){ myTimer()},1000);
        function  myTimer(){
                var d = new Date();
                var t=d.toLocaleTimeString();
                document.getElementById('demo').innerHTML=t;
        }
function stop(){
   <html>
<body>
<p id="demo" ></p>
<p id="demo2" onclick="stop()">stop</p>
<script type="text/javascript">
var temp=setInterval(function(){ myTimer()},1000);
        function  myTimer(){
                var d = new Date();
                var t=d.toLocaleTimeString();
                document.getElementById('demo').innerHTML=t;
        }
function stop(){
        clearInterval(temp);
}
</script>
</body>
</html>

}
</script>
</body>
</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.