简体   繁体   English

如何使用javascript / jquery在30分钟内没有使用表单字段时显示警告消息?

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

I have a form with couple of input fields and i want a script that will show a pop up message IF no field is used after a certain time-frame, asking "Are you still there?" 我有一个带有几个输入字段的表单,我想要一个显示弹出消息的脚本,如果在某个时间范围后没有使用任何字段,则询问“你还在吗?” or something like that. 或类似的东西。

Is it possible? 可能吗? Please advice, thanks! 请指教,谢谢!

You'll want to start a timer for thirty minutes and attach a change event to all of your fields. 您需要启动计时器30分钟,并将change事件附加到您的所有字段。 On change, you want to erase the current timer, and create a new one for another 30 minutes. 在更改时,您要删除当前计时器,并再创建一个新计时器30分钟。

For example, it would be something like this: 例如,它会是这样的:

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

And for the timer, you'd just use settimeout 对于计时器,你只需使用settimeout

Here is full sample in JsFiddle: 以下是JsFiddle中的完整示例:

What I recommend is using setTimeOut to call a method that you want to display a message or do whatever action you wanted to do. 我建议使用setTimeOut来调用要显示消息的方法或执行您想要执行的操作。 Then on change event on all your input types try to reset the TimeOut. 然后在所有输入类型的更改事件上尝试重置TimeOut。 Here is JS Code that simulates this scenario: 这是模拟这种情况的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?");
}

You can change $("input") to any criteria that may fit your project. 您可以将$(“输入”)更改为适合您项目的任何条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何添加 JavaScript 警报消息以显示使用 JavaScript 的 html 表单验证是正确的 - How to add a JavaScript alert message to show that the html form validation is correct using JavaScript 如何使用 jQuery 在 30 分钟内使 cookie 过期? - How to expire a cookie in 30 minutes using jQuery? 如何使用Symfony2中的警报消息通过javascript显示到html树枝表单字段值? - how to display to a html twig form field value through javascript using an alert message in Symfony2? 使用jQuery输入太短时显示警报消息 - Show Alert Message When Input Is Too Short using jQuery 如何使用jQuery在隐藏字段中显示消息? - How to show message in hidden field using jQuery? 如果使用 JavaScript 未找到结果,如何显示“未找到结果”警报消息? - How to show "No result found" alert message if no result found using JavaScript? 如何使用JavaScript选中复选框显示警报消息? - How to show alert message using javascript to check checkbox? 如何使用Javascript放置我的PHP联系人表单警报消息? - How to position my PHP contact form alert message using Javascript? 如何通过使用JQuery而不是Javascript显示警报消息? - How to display an alert message by using JQuery instead than Javascript? 使用ajax将数据插入数据库时​​如何显示警报消息? - How to show an alert message when data is inserted to database using ajax?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM