[英]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.