函数过程完成时的Javascript警报

函数过程完成时的Javascript警报,javascript,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

下面是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_获取_预览功能代码:

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下方显示消息功能:

  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\u获得预览时,我要提醒的内容;过程已经完成。我已经尝试过了,但每次都会先显示警报,然后再加载函数

我怎样才能做到这一点


提前感谢。

setTimeoutfunction{…},0只需在当前调用堆栈完成执行后将要运行的代码排队即可。因此,您面临的问题是因为您的>ref.msglist\u get\u预览;也在执行异步任务,这就是为什么它在setTimeout之后排队,因此您得到警报“完成”;首先,然后是方法执行。 以下是解释相同问题的示例:

this.preview\u timer=setTimeoutfunction{ abc; 警报“完成”; }, 500; 功能abc{ setTimeoutfunction{ 警惕“abc”; },100 }
这表明ref.msglist\u get\u预览是异步的。。。关于它的作用和返回的内容有什么线索吗?您是否考虑过将警报设置为“完成”;在msglist_get_preview函数的末尾,您正在使用setTimeout。一旦超时被重新设置,它将触发警报。相反,您应该在流程结束时调用它complete@Snappy-好的,在执行警报之前,您需要等待它加载的消息完成加载-否则您将在它加载的消息作为messageOk加载完成之前收到警报,因此它不是异步函数,但您如何确定该过程何时完成?看起来您是在告诉浏览器转到一个新链接,所以您是否希望只有在加载页面后才会显示警报?如果在调用ref.msglist\u get\u preview;后立即设置超时功能,有什么区别;。它的工作原理是一样的,不需要额外的代码;已经解决了。在ref.msglist\u get\u预览中设置超时;只是为了显示进一步XYZ的目的。。方法中发生的异步操作。您不需要将settimeout放入ref.msglist\u get\u preview;方法,那么它也会起作用。函数msglist_get_preview{返回新的Promisefunctionresolve,拒绝{//你的东西…resolveStuff工作了!;};}