簡體   English   中英

如何使用javascript / jquery在30分鍾內沒有使用表單字段時顯示警告消息?

[英]How to show an alert message when no form field is used in 30 minutes using javascript/jquery?

我有一個帶有幾個輸入字段的表單,我想要一個顯示彈出消息的腳本,如果在某個時間范圍后沒有使用任何字段,則詢問“你還在嗎?” 或類似的東西。

可能嗎? 請指教,謝謝!

您需要啟動計時器30分鍾,並將change事件附加到您的所有字段。 在更改時,您要刪除當前計時器,並再創建一個新計時器30分鍾。

例如,它會是這樣的:

$('input[type=text]').bind('change', function (e) {
    // .. Triggered every time a form text field is changed
    // .. Do stuff here
});

對於計時器,你只需使用settimeout

以下是JsFiddle中的完整示例:

我建議使用setTimeOut來調用要顯示消息的方法或執行您想要執行的操作。 然后在所有輸入類型的更改事件上嘗試重置TimeOut。 這是模擬這種情況的JS代碼:

// Timer for 3 seconds to call formTimeOut function 
var _timer = setTimeout("formTimeOut()",3000);

$(function(){

    // Monitor Value change of Inputs in your HTML
    $("input").change(function(){

        // Clear previous timer 
        clearTimeout(_timer);
        // Reset the timer to re-run after 3 seconds.
        _timer = setTimeout("formTimeOut()",3000);

    });
});

// This is the function that will run after time out
function formTimeOut(){

    // Timeout Logic goes here
    alert("Are you there?");
}

您可以將$(“輸入”)更改為適合您項目的任何條件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM