Jquery Idle timer active not firing

随声附和 提交于 2019-12-12 23:25:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!