jQuery event.timeStamp 屬性



jQuery 的event.timeStamp 屬性用於獲取從 1970 年 1 月 1 日到事件發生時所經過的毫秒數。

為什麼是 1970 年 1 月 1 日,而不是其他日期?因為 1970 年 1 月 1 日被廣泛認為是 UNIX 系統的“紀元日期”,標誌著這些系統在計算術語中時間的開始。

語法

以下是 jQuery event.timeStamp 屬性的語法:

event.timeStamp

引數

此屬性不接受任何引數。

返回值

此屬性返回事件發生時自 1970 年 1 月 1 日以來的毫秒數。

示例 1

以下程式演示了 jQuery event.timeStamp 屬性的使用:

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
   <style>
    p{
        width: 500px;
        padding: 10px;
        background-color: green;
        color: white;
    }
   </style>
</head>
<body>
    <p>Click on me to count the number of milliseconds since Jan 1, 1970, when the click event occurred.</p>
    <script>
        $('p').click(function(event){
            alert("The click event occured number of " + event.timeStamp + " since Jan 1, 1970");
        });
    </script>
</body>
</html>

輸出

以上程式顯示了一條訊息,當用戶點選它時,會彈出一個警報框,顯示自 1970 年 1 月 1 日以來點選事件發生時所經過的毫秒數:


示例 2

以下是 jQuery event.timeStamp 屬性的另一個示例。使用此屬性,我們計算自 1970 年 1 月 1 日以來 mouseover 事件發生時所經過的毫秒數:

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
    <style>
        button{
            width: 200px;
            padding: 10px;
            background-color: green;
            color: white;
        }
    </style>
</head>
<body>
    <p>Hover on the below button to count the number of milliseconds since Jan 1, 1970, when the mouseover event occurred.</p>
    <button>Hover on me!</button>
    <span></span>
    <script>
        $(document).ready(function(){
            $('button').mouseover(function(event){
                $('span').text("The mouseover event occured number of " + event.timeStamp + " milliseconds since Jan 1, 1970");
            });
        });
    </script>
</body>
</html>

輸出

執行程式後,將顯示一個按鈕。當滑鼠指標懸停在此按鈕上時,將在按鈕旁邊顯示自 1970 年 1 月 1 日以來滑鼠懸停事件發生時所經過的毫秒數:


jquery_ref_events.htm
廣告

© . All rights reserved.