Home > Net >  In react setInterval and again setInterval. I want to check if user is active on the page or not
In react setInterval and again setInterval. I want to check if user is active on the page or not

Time:10-23

I want to setInterval again and again in react. I want to check if user is active on the page or not. if he is typing then I can make him active and if not then I can close the connection.

https://devforhelp.com/chat

CodePudding user response:

You can use setTimeOut() for check user typing.

useEffect(() => {
    let timeout;

    refInput.current.addEventListener('keypress', () => { /* user is typing */
        if (timeout) clearTimeout(timeout);
        userIsActive();

        timeout = setTimeout(() => {
            userIsNotActive();
        }, 30000 /* 30 second */);
    });
}, []);
  • Related