Untitled

mail@pastecode.io avatar
unknown
javascript
a year ago
1.5 kB
2
Indexable
Never
<!doctype html>
<html>

<head>
    <title>jQuery Counter</title>
    <script src="https://www.tutorialspoint.com/jquery/jquery-3.6.0.js"></script>
</head>

<body>
    <h1>jQuery Counter </h1>

</body>
<script>
    var activeFlag;
    var resetInterVal;
    var activeTime = 0;
    
    function logActiveTime(){

        console.log("started....");

        if(activeFlag){
            clearInterval(activeFlag);
        }

        //every second increze activity time by 1 second
        activeFlag = setInterval(function() {
            activeTime++;
            console.log("activeTime",activeTime);
        },1000);

        //Reset setting and adding 10 min again
        resetCounting();
    }

    // Resetting timer if any activity in 10 Min.
    function resetCounting(){
        console.log("Timer Reset");

        if(resetInterVal){
            clearInterval(resetInterVal);
        }

        // For testing added 10 second to clear
        resetInterVal = setInterval(function() {
            clearActiveTime();
        },10000); // Replace by //600000
    }

    // clear all timer if there is no activity in 10 Min.
    function clearActiveTime(){
        if(activeFlag){
            clearInterval(activeFlag);
        }

        if(resetInterVal){
            clearInterval(resetInterVal);
        }
    }

    $(document).click(function() {
        //Resuming couting on activity 

        logActiveTime();
    });

    //Start couting on page load 
    logActiveTime();
</script>

</html>