If no activity for X seconds, run function that toggles classname. If activity, stop toggle function and reset inactivity timer

user3092

After 100 seconds of inactivity, I want to run a function that toggles a classname "show" every 10 seconds. (The 'show' classname makes a modal appear).

If/when there is user activity, I want to remove the 'show' classname (if it's currently toggled on) and also stop the 10-second toggle cycling function. I want this toggle cycling to run again only after there's been inactivity for 100 seconds.

I can't seem to get the toggle cycling function to stop upon user activity. The toggle cycle just continues to run. Any help would be appreciated! Here's what I have:

var el = document.querySelector('#element');
var toggle = function() {
    el.classList.toggle('show');
}

function toggleTimer() {
    var u;
    window.onload = resetTimer;
    window.onmousemove = resetTimer;
    window.onmousedown = resetTimer;
    window.ontouchstart = resetTimer;
    window.onclick = resetTimer;
    window.onkeypress = resetTimer;
    window.addEventListener('scroll', resetTimer, true); 

    function toggleCycle() {
        setInterval(toggle, 10000);
    }

    function resetTimer() {
        clearTimeout(u);
        u = setTimeout(toggleCycle, 100000);
        el.classList.remove('show');
        clearInterval(toggleCycle);
    }
}

toggleTimer();
Kosh

You might use CSS animation added to .show instead of handling setInterval.
Also your code might be simpler (see the snippet below):

Please note that I reduced times not to have you waiting for 100 secs.

var el = document.querySelector('#element');
var timer = 5000, tick = 1000;
const resetTimer = () => (timer = 5000) && el.classList.remove('show');
  
['load', 'touchstart', 'mousedown', 'mousemove', 'keydown', 'scroll']
.forEach(e => document.addEventListener(e, resetTimer));

setInterval(() => (timer -= tick) || el.classList.add('show'), tick);
h1:not(.show) {display: none}

.show {animation: togg 2s linear infinite alternate}

@keyframes togg {
  40% {opacity:1}
  50% {opacity:0}
  100% {opacity:0}
}
<h1 id="element">SHOW</h1>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Javascript + Run a function for X seconds

Run a Python function for x seconds?

Timer does not stop at old activity

vuejs call function when no activity for 5 seconds after typing in text

Timer triggered Azure function Activity.Current is null

Django - run a function every x seconds

How to run a JavaScript function after x seconds

flutter run function every x amount of seconds

Angular 6 run a function in every X seconds

Jquery: run function only every x seconds

Login function with activity indicator

setting timeout for async function to stop running after x seconds if not done by then

Run Function Every "X" hours for "X" Seconds over 24 hours

Launching activity from the background, doesn't run the function in onResume()

Call activity function from another activity

Text toggle function only toggles the text a singular time

Using a timer to count seconds and update a string within the activity

Function unresolved reference in main activity

Calling function by string name in activity

How to pass an Activity as parameter in function

Dependence between Activity and createpdf function

Call function from different activity

Run function every 3 seconds

Run function every 10 seconds

How to get Azure Functions to run a durable function that dynamically fetches the activity function

Inactivity and activity ,application idle , user-inactivity auto logout

programming EXACT timer which calles a function in c language every x seconds

How to reset and set up timer function?

timer doesn't reset in sfml function