MagicMirror Forum
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • 3rd-Party-Modules
    • Donate
    • Discord
    • Register
    • Login
    A New Chapter for MagicMirror: The Community Takes the Lead
    Read the statement by Michael Teeuw here.

    Need help in Javascript for resetting module if user no longer interacts after x seconds.

    Scheduled Pinned Locked Moved Solved Troubleshooting
    3 Posts 3 Posters 1.0k Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J Offline
      jchariot
      last edited by jchariot

      I am using a Sonar module on Raspberry Pi to indicate whether someone is in front of the mirror or not. I use MMM-websocket to transfer this information to MagicMirror and it works correctly. Now, when there is nobody in front of the mirror for x seconds, I would like to call a function to reset the module. However, I got an issue on Javascript side:

      Currently I have this function for resetting in my module:

      userNoLongerInFrontSoReset: function(){
          if (this.resetTimeout != null){
              clearTimeout(this.resetTimeout);
          }
      
          this.resetTimeout = setTimeout(function(){
              console.log("resetTimeout setTimeout");
      
              this.myVar = null;
              this.myVar2 = null;
              this.myVar3 = null;
      
              this.sendNotification("PAGE_CHANGED", 0); 
      
              this.updateDom();
      
          }, 3000)
      }
      

      I see from the console that resetTimeout setTimeout is being printed out from the code above, but then it got:

      Uncaught TypeError: this.sendNotification is not a function at myOwnModule.js:xxx
      

      So this is the problem. All those variables myVar, etc. are not being referred correctly as well as the function sendNotification. How can I set all myVar and call sendNotification from the anonymous function in this case? Or is there a better way to do this?

      Thank you,

      ? 1 Reply Last reply Reply Quote 0
      • ? Offline
        A Former User @jchariot
        last edited by A Former User

        @jchariot
        Change this;

        this.resetTimeout = setTimeout(function(){
         ...
          this.sendNotification("PAGE_CHANGED", 0); 
          this.updateDom();
          ...
        

        to this;

        this.resetTimeout = setTimeout(()=>{
         ...
          this.sendNotification("PAGE_CHANGED", 0); 
          this.updateDom();
          ...
        

        Or this;

        var module = this
        this.resetTimeout = setTimeout(function(){
         ...
          module.sendNotification("PAGE_CHANGED", 0); 
          module.updateDom();
          ...
        

        Inside of function(){...}, this will be function itself, so you cannot access this - referer to module - of upper level.
        To avoid, you can use ES6 style anonymous function ()=>{} instead function(){} or assign prior this to another temporal object.

        strawberry 3.141S 1 Reply Last reply Reply Quote 3
        • strawberry 3.141S Offline
          strawberry 3.141 Project Sponsor Module Developer @Guest
          last edited by

          @sean or

          this.resetTimeout = setTimeout(function(){
           ...
            this.sendNotification("PAGE_CHANGED", 0); 
            this.updateDom();
            ...
          }.bind(this));
          

          Please create a github issue if you need help, so I can keep track

          1 Reply Last reply Reply Quote 1

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          • 1 / 1
          • First post
            Last post
          Enjoying MagicMirror? Please consider a donation!
          MagicMirror created by Michael Teeuw.
          Forum managed by Sam, technical setup by Karsten.
          This forum is using NodeBB as its core | Contributors
          Contact | Privacy Policy