本文介绍了jQuery的空闲计时器活动不触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JQuery空闲时间插件在x分钟的空闲时间后自动注销用户.但是,该空闲/活动时间也应在服务器中更新.因此,尝试对active.idleTimer函数内部的服务器执行Ajax攻击.

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>

但是,当用户处于活动状态时,不会调用此active.idleTimer.我在这里做什么错了?

But this active.idleTimer is not called when the user is active. What am I doing wrong here?

推荐答案

似乎对我有用:

尝试将代码放入jQM pageinit事件中,并使用.on()代替bind:

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());
    });
});

这篇关于jQuery的空闲计时器活动不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 19:47