如何使用 javascript/jquery 在 30 分钟内没有使用表单字段时显示警报消息?
我有一个带有几个输入字段的表单,我想要一个脚本,如果在一定时间范围后没有使用任何字段,则会显示一条弹出消息,询问“你还在吗?”或类似的东西。
是否可以?请指教,谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要启动一个三十分钟的计时器,并将
change
事件附加到您的所有字段。更改时,您希望清除当前计时器,并创建一个新计时器,再计时 30 分钟。例如,它会是这样的:
对于计时器,您只需使用
settimeout
You'll want to start a timer for thirty minutes and attach a
change
event to all of your fields. On change, you want to erase the current timer, and create a new one for another 30 minutes.For example, it would be something like this:
And for the timer, you'd just use
settimeout
这是 JsFiddle 中的完整示例:
我建议使用 setTimeOut 来调用您想要显示消息或执行任何您想要执行的操作的方法。然后,在所有输入类型的更改事件上尝试重置超时。以下是模拟此场景的 JS 代码:
您可以将 $("input") 更改为可能适合您的项目的任何条件。
Here is full sample in 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. Then on change event on all your input types try to reset the TimeOut. Here is JS Code that simulates this scenario:
You can change $("input") to any criteria that may fit your project.