JavaScript Set time out event on a m() generated element

I have a m("p.help") element which is removed with a click event.

I also want the element to be removed automatically after a few seconds if not clicked. I need to set a time out on it. Setting time out does not work.

function help() {
  var text = `This is a service template. Use Service section to set the schedule`;

  var node = m("p.help", {
    onclick() {
      this.parentNode.removeChild(this);
    },
  }, text);

  setTimeout(() => {
    if (node.parentNode) {
      node.parentNode.removeChild(node);
      console.log("removed");
      m.redraw();
    }
  }, 5000);

  return node;
}

The click event works fine but the time out does not work. It is not even triggered judging by the console.log()

What am I doing wrong?

EDIT

Thanks ciscoheat for the tip.

I had to put the timer in the controller for this to work.

So this one works fine:

function controller(init) {
  this.display = {
    help: true
  };
  setTimeout(() => {
    this.display.help = false;
    m.redraw();
  }, 5000);
}

function view(vm) {
  return m(".container", [
    (() => {
      var text = "Some text";
      if (vm.display.help) {
        return m("p.memo", {
          onclick() {
            this.parentNode.removeChild(this);
          }
        }, text);
      }
    })(),
  ]);
}
Answer:1

This is the result: This is what the page shows when the JSON sets childnode.state.selected For example: {"children":[],"id":37,"text":"应用显 示","orderid":1,"parent":36,"state":{"disabled":false,"...

This is the result: This is what the page shows when the JSON sets childnode.state.selected For example: {"children":[],"id":37,"text":"应用显 示","orderid":1,"parent":36,"state":{"disabled":false,"...

I have a container that is a certain width. I have two select elements rendering on the same line in this main div container. The first one is absolute positioned 40px left from the main div ...

I have a container that is a certain width. I have two select elements rendering on the same line in this main div container. The first one is absolute positioned 40px left from the main div ...

Is there any way in which I can spy on the bunyan log to ensure I print out what I expect? MyFile.js const bunyan = require('bunyan'); const log = bunyan.createLogger({name: 'FailureAuditService'}); ...

Is there any way in which I can spy on the bunyan log to ensure I print out what I expect? MyFile.js const bunyan = require('bunyan'); const log = bunyan.createLogger({name: 'FailureAuditService'}); ...

I have heard that JavaScript has a function called search() that can search for a string ( lets call it A ) in another string ( B ) and it will return the first position at which A was found in B. ...

I have heard that JavaScript has a function called search() that can search for a string ( lets call it A ) in another string ( B ) and it will return the first position at which A was found in B. ...

  1. find second occurrence of character in string python
  2. find second occurrence of character in string excel
  3. find second occurrence of character in string sql
  4. find second occurrence of character in string
  5. find second occurrence of character in string c++
  6. find second occurrence of character in string javascript
  7. find second occurrence excel
  8. find second occurrence of character in string r
  9. find second occurrence of character in string regex
  10. find second occurrence of character in excel
  11. find second occurrence of character in string alteryx
  12. find second occurrence of element in list python
  13. find second occurrence of character in string php
  14. find second occurrence of character in string mysql
  15. find second occurrence of character in string oracle
  16. find second occurrence of character in string vb.net
  17. find second occurrence of character in string powershell
  18. find second occurrence of character in string sas
  19. find second occurrence of character in string access
  20. find second occurrence of character in string jquery