问题
I'm using JQuery idle-time plugin to automatically logs out a user after x mins of idle time. But this idle/active time should be updated in the server as well. So trying to do an Ajax hit to the server inside the active.idleTimer function.
<script type="text/javascript">
(function ($) {
var timeout = 1800000;
$(document).bind("idle.idleTimer", function () {
window.location.href = "MyLOGOUT_URL";
});
$(document).bind("active.idleTimer", function () {
$.ajax({ type: "POST", url : "My_URL" });
});
$.idleTimer(timeout);
})(jQuery);
</script>
But this active.idleTimer is not called when the user is active. What am I doing wrong here?
回答1:
Seems to work for me:
DEMO
Try putting your code in a jQM pageinit event and use .on() instead of bind:
$(document).on("pageinit", "#page1", function(){
$( document ).idleTimer( 5000 );
$( document ).on( "idle.idleTimer", function(){
$("#textinput-1").val('Idle at: ' + new Date());
});
$( document ).on( "active.idleTimer", function(){
$("#textinput-1").val('Active again at: ' + new Date());
});
});
来源:https://stackoverflow.com/questions/21193593/jquery-idle-timer-active-not-firing