1

我使用 jQuery Mobile、骨干网和常规 ole javascript 计时器遇到了麻烦。Web 应用程序有一个空闲计时器,它在两个连续页面上运行。问题是我当前处理事件的方式(将所有内容包装在 pageshow 中),我的测试显示除了在后续 pageshow 事件上实例化的第二个计时器之外,第一页的计时器继续运行 - 导致闪烁会话倒计时(同时显示倒计时变量的两个实例)。

我试图检查是否idleInterval已经存在无济于事(结果总是它不存在)。

我想我可以监听其他“pagechange”事件之一——比如在开始时触发并停止前一个计时器的事件——但是我必须将代码的不同部分包装在不同的“pagechange”事件中,因此有范围问题(在任何一种情况下都需要参考相同的主干模型)。真是一团糟...

$(document).on('pageinit pageshow', 'div:jqmData(role="page"), div:jqmData(role="dialog")', function (event) {
    if($(this).attr("id")!="KioskDefaultLogin" && $(this).hasClass('kiosk-mode') && event.type=='pageshow') {

       ...backbone js code...

       function sessionTimeout() {

        var inactivetime = 0;
        console.log('TIMER EXISTS?=>'+window.hasOwnProperty('idleInterval'));

        if( qnumerItem.get("state") != "login") {

            var idleInterval = setInterval(function(){

                if(qnumerItem.get("state") != "timeout") {

                    inactivetime ++;
                    console.log('TIMER -----'+inactivetime+'------');
                }

                ////console.log("timer 1: "+inactivetime);
                /*Edit the inactivetime value here to change the idle session timer*/
                if (inactivetime == 15 && qnumerItem.get("state") != "timeout") {

                    qnumerItem.changeState("timeout");
                    inactivetime=0;

                    /*End Session Due to Inactivity Countdown*/
                    $(function(){
                        var count=30;
                        console.log("I STARTED");
                        var countdown = setInterval(function(){

                            //$(".ui-page-active .session-countdown").html(count);
                            //$(".ui-page-active .to-phrase").show();
                            if (count == 0) {
                                clearInterval(countdown);
                                qnumerItem.changeState("kill");
                            }
                            count--;
                            console.log("timer 1: "+inactivetime);
                            console.log("timer 2: "+count);
                        }, 1000);

                        $(this).on( 'click', '.to-continue', function(event) {
                            event.preventDefault();
                            clearInterval(countdown);
                            $("#session-countdown").html(60);
                        });
                        $(this).on( 'click', '.to-end', function(event) {
                            event.preventDefault();
                            clearInterval(countdown);
                        });
                    });
                }
            }, 1000); // 1 second
        }

        //Zero the idle timer on mouse movement.
        document.addEventListener('touchstart', function(e) {
            e.preventDefault();
            inactivetime = 0;
        }, false);

        document.addEventListener('touchmove',
            function (e) {
                e.preventDefault();
            }, false);

        $(this).mousemove(function (e) {
            inactivetime = 0;
        });
        $(this).keypress(function (e) {
            inactivetime = 0;
        });
        $(this).click(function (e) {
            inactivetime = 0;
        });

    }

        sessionTimeout();

...more code....
4

0 回答 0