Home > Net >  How to remove element from dom after few seconds in ember
How to remove element from dom after few seconds in ember

Time:01-31

I want to show this element only for 5 seconds and after that needs to remove from the dom.

{{#if canShow}}
  <div>
     This is block
  </div>
{{/if}}

Please help me to solve. I don't know how to achieve this.

CodePudding user response:

I think you can do it by first creating the element in the DOM then using a timer to hide after a certain amount of time.

let helloWorld = document.createElement('div');

helloWorld.innerHTML = 'Hello World';

document.body.appendChild(helloWorld);

setTimeout(function() {
  document.body.removeChild(helloWorld);
}, 5000);

CodePudding user response:

In Ember.js, you can use the Ember.run.later method to remove an element from the DOM after a certain number of seconds. The following code demonstrates how to remove a DOM element with an id of "my-element" after 5 seconds:

Ember.run.later(function() {
 Ember.$('#my-element').remove();
}, 5000);

This code uses the Ember.$ method to select the element with an id of "my-element", and the remove method to remove it from the DOM. The Ember.run.later method is used to schedule the removal to occur 5 seconds (5000 milliseconds) later.

  • Related