函数进程完成时的 JavaScript 警报



这是Javascript代码:

if (selected && this.env.contentframe && !list.multi_selecting)
this.preview_timer = setTimeout(function() {
ref.msglist_get_preview();
alert('done');
}, list.dblclick_time);
else if (this.env.contentframe)
this.show_contentframe(false);

以下是msglist_get_preview()函数代码:

this.msglist_get_preview = function()
{
var uid = this.get_single_uid();
if (uid && this.env.contentframe && !this.drag_active)
this.show_message(uid, false, true);
};

在 id 下面是show_message()函数:

this.show_message = function(id, safe, preview)
{
if (!id)
return;
var win, target = window,
url = this.params_from_uid(id, {_caps: this.browser_capabilities()});
if (preview && (win = this.get_frame_window(this.env.contentframe))) {
target = win;
url._framed = 1;
}
url = this.url(preview ? 'preview': 'show', url);
this.location_href(url, target, true);
}:

当函数ref.msglist_get_preview();进程完成时,我想提醒什么。我已经尝试过,但每次警报首先出现然后加载函数。

我怎样才能做到这一点?

提前谢谢。

setTimeout(function(({...}, 0( 只是将代码排队,以便在当前调用堆栈完成执行后运行。所以你面临的问题是因为你的>ref.msglist_get_preview((;也在执行异步任务,这就是为什么它在 setTimeout 后排队,因此您会收到警报("完成"(;首先是方法执行。 以下是解释相同内容的示例:

this.preview_timer = setTimeout(function() {
abc();
alert('done');
}, 500);
function abc(){
setTimeout(function(){
alert('abc');
},100) 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

这里警报('abc'(将在提醒('完成'(之后执行

对于您的问题,您可以使用javascript承诺。以下代码应该可以解决您的问题:

if (selected && this.env.contentframe && !list.multi_selecting)
this.preview_timer = setTimeout(function() {
ref.msglist_get_preview().then(function(){
alert('done');
})
}, list.dblclick_time);
else if (this.env.contentframe)
this.show_contentframe(false);
function msglist_get_preview() {
return new Promise(function(resolve, reject) {
setTimeout((function() {
alert('msglist_get_preview worked');
resolve("Stuff worked!");
}), 1000);
});
}

最新更新