Search code examples
node.jsreactjssocket.iotimeoutsetinterval

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


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


Solution

  • 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 */);
        });
    }, []);