0

After implementing Detecting idle time in JavaScript elegantly I get "WebSocket is already in CLOSING or CLOSED state" error in the browser console. How to fix this issue? Here is my code:

    var inactivityTime = function () {
        var t;
        window.onload = resetTimer;
        document.onmousemove = resetTimer;
        document.onkeypress = resetTimer;

        function detector() {
            alert("You are idle!");
        }


        function resetTimer() {
            console.log("RESET!");
            clearTimeout(t);
            t = setTimeout(detector, 10000)

            // 1000 milisec = 1 sec
        }
    };

Template.myTemplate.onRendered(function(){
    inactivityTime();
});
Community
  • 1
  • 1

1 Answers1

0

You are calling clearTimeout(t) when t may not have been initialised - you should check for a value first

Mikkel
  • 7,693
  • 3
  • 17
  • 31
  • I checked it with if(), but it doesn't help: `if(t) clearTimeout(t);` –  Oct 27 '16 at 08:14
  • OK, I fixed this issue. Just changed set/clearTimeout to Meteor.set/clearTimeout. However, I will accept your answer because it was helpful too. –  Oct 27 '16 at 09:17
  • Hmm, someone cheered too soon :( Error is still there. –  Oct 27 '16 at 09:33
  • One minute it works, then it fails again? What does your code look like now – Mikkel Oct 27 '16 at 11:36
  • My Code is the same. I get no error if the alert message appears (first timeOut) and I click OK (alert message). However, if the alert message appears and I wait for the second timeout without clicking OK, I get this error. If I could disable second Timeout so long as I didn't click OK, it will maybe fix this issue. But, I don't know how to do it. –  Oct 27 '16 at 12:30
  • Right, that makes sense. Using alert isn't a great practice, especially for something like this, as it is interrupting the flow. Just change it to a console.log(...) and everything should be sweet – Mikkel Oct 27 '16 at 19:57