开发者

Execute function if user doesn't press button with x seconds

开发者 https://www.devze.com 2023-03-12 04:16 出处:网络
I\'m using a Jquery UI dialog like so: var $dialog; $(document).ready(function() { $dialog = $(\'<div></div>\')

I'm using a Jquery UI dialog like so:

    var $dialog;
$(document).ready(function() {
    $dialog = $('<div></div>')
        .html('Are you awake?<br/><br/><button>Yes</button>')
        .dialog({
            autoOpen: false,
      开发者_如何学编程      title: 'Inactivity Monitor'
        });

});

This dialog shows every 20 minutes and the user must click 'yes' to continue. I need to have something running behind this so if the user doesn't click 'yes' within 10 minutes, another function is executed (send email to head office).

How could I do this?

Thanks for any help


You could use a setTimeout(); that will execute that function if reached, and you can set it to a variable so you could disable the timer if the button was clicked. I've set up a jsfiddle to show you how: http://jsfiddle.net/SrwTP/

JavaScript:

var buttonTimer = setTimeout("alert('Oh noes! you didnt click the button :(');", 5000);
document.getElementById("timerButton").onclick = function(){ clearTimeout(buttonTimer); };

HTML:

<button id="timerButton">Click me or an alert will popup in 5 seconds!</button>


You can use setTimeout to get this done. Have a flag variable to identify the user-actions and validate it in the interval-function for sending the email.

For ex:

The follow example will show the alert box for every 3 seconds.

<html>
<head>
<script type="text/javascript">
function timeMsg()
{
var t=setTimeout("alertMsg()",3000);
}
function alertMsg()
{
alert("Hello");
}
</script>
</head>

<body>
<form>
<input type="button" value="Display alert box in 3 seconds"
onclick="timeMsg()" />
</form>
</body>
</html>
0

精彩评论

暂无评论...
验证码 换一张
取 消