gpt4 book ai didi

JavaScript 闪现消息

转载 作者:行者123 更新时间:2023-11-28 16:06:07 26 4
gpt4 key购买 nike

我有一个主干应用程序,可以将公告呈现到 dom 中,但是,我希望公告在几秒钟后消失。我猜测,在显示公告后,我应该调用一个将其删除的函数,但是有没有办法延迟其执行,或者在显示模板后创建回调。抱歉,如果这是一个简单的问题。

   $(this.el).html(this.template({ announcement: announcement}));

this.removeAnnouncement();
},

removeAnnounce: function(){
this.$el.remove();
},

最佳答案

您可以使用setTimeout:

window.setTimeout(_.bind(this.removeAnnounce, this), 2000);

这将在 2000 毫秒(或 2 秒)后调用 this.removeAnnounce。请注意,不要在 this.removeAnnounce 之后添加 (),因为您实际上传递的是函数引用。超时将在时间过去后调用该函数。

编辑:_.bind部分确保超时调用函数时正确分配作用域中的this。谢谢@PaulHoenecke

关于JavaScript 闪现消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14867971/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com