简体   繁体   中英

Function activity time out don't works

This is my activity timeout function, but don't works. I need help to know why, thanks!

First the function asks to user if he/she is here:

function activityTimeout(){
        //$("#jquery_jplayer").jPlayer("pause");

        clearTimeout(activityTO);

        blockInfoMsg('Are you here?.<br>(Automatic quit in <span id="spanActTO">60</span>seconds)<br><br><input type="button" value="Im here!" onclick="javascript:renewActivityTimeoutUnblock()" class="inputButton">&nbsp;or&nbsp;<input type="button" value="Quit Now!" onclick="javascript:abandonCorrection()" class="inputButton">', 0);

        popupTO = setTimeout(abandonCorrection, 60000);
    }

If the user is here then he/she is back to work and the function reset the timeout countdown:

function renewActivityTimeoutUnblock(){
        $.unblockUI();

        renewActivityTimeout();
    }

    function renewActivityTimeout(){
        clearTimeout(activityTO);
        clearTimeout(popupTO);

        activityTO = setTimeout(activityTimeout, 1800000);
    }

You can not clearTimeout and setTimeout in the same function (for the same timeout) because the function executed once, when the function is called.

function renewActivityTimeoutUnblock(){
    $.unblockUI();

    renewActivityTimeout();
}

function stopTimeout(){
    clearTimeout(activityTO);
}

function renewActivityTimeout(){
    clearTimeout(popupTO);

    activityTO = setTimeout(activityTimeout, 1800000);
}

maybe you it's didn't work because you might be having this error Uncaught ReferenceError: popupTO is not defined in your javascript console.

Make sure those variables that you store the timeouts are declared.

function activityTimeout(){
    window.clearTimeout(activityTO);

    // make sure that this is an existing function in you js code
    blockInfoMsg('Are you here?.<br>(Automatic quit in <span id="spanActTO">60</span>seconds)<br><br><input type="button" value="Im here!" onclick="javascript:renewActivityTimeoutUnblock()" class="inputButton">&nbsp;or&nbsp;<input type="button" value="Quit Now!" onclick="javascript:abandonCorrection()" class="inputButton">', 0);

    // make sure abandonCorrection is an existing function, and this will timeout for 1 minute
    popupTO = window.setTimeout(abandonCorrection, 60000);
}

function renewActivityTimeoutUnblock(){
    $.unblockUI();
    renewActivityTimeout();
}
var activityTo;  // declare this variable like this
var popupTo;   // declare this variable like this

function renewActivityTimeout(){
    window.clearTimeout(activityTO);
    window.clearTimeout(popupTO);

    // take note of your time here it's 1,800,000 ms that's about 30 minutes
    activityTO = window.setTimeout(activityTimeout, 1800000);
}

Take note of your time, the activityTimeout will kick in after 30 minutes. Try to lower it so that you can see. Then use alerts or console log for your own debugging.

Here is a short demo of the timeout working , it's delayed for 3 seconds before a console.log and alert pops up. Hope this helps you.

NOTE: Don't confuse setTimeout with setInterval.

setTimeout is just a delay, and executes only once. Read more about it here

setInterval on the other hand is like a loop, that will keep on calling the function during every time interval you set Read more about it here

To learn more about window timers just go here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM