简体   繁体   English

如何在余烬中几秒钟后从dom中删除元素

[英]How to remove element from dom after few seconds in ember

I want to show this element only for 5 seconds and after that needs to remove from the dom.我只想显示此元素 5 秒,然后需要从 dom 中删除。

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

Please help me to solve.请帮我解决。 I don't know how to achieve this.我不知道如何实现这一目标。

In Ember.js, you can use the Ember.run.later method to remove an element from the DOM after a certain number of seconds.在 Ember.js 中,您可以使用 Ember.run.later 方法在一定秒数后从 DOM 中删除一个元素。 The following code demonstrates how to remove a DOM element with an id of "my-element" after 5 seconds:以下代码演示了如何在 5 秒后删除 id 为“my-element”的 DOM 元素:

Ember.run.later(() => {
  // Remove the element from the DOM
  document.querySelector('#my-element').remove();
}, 5000); // runs after 5 seconds

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.此代码使用 Ember.$ 方法将 id 为“my-element”的元素添加到 select,并使用 remove 方法将其从 DOM 中删除。 The Ember.run.later method is used to schedule the removal to occur 5 seconds (5000 milliseconds) later. Ember.run.later 方法用于安排删除在 5 秒(5000 毫秒)后发生。

If you are in a component you can use:如果你在一个组件中,你可以使用:

 init() { Ember.run.later(() => { this.set('canView', false) // Or you can toggleProperty here }, 5000); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM