2010-07-14 62 views
0

我正在嘗試添加一個jQuery UI模式對話框以顯示函數何時啓動,其中顯示「Please wait」。然後在功能完成時關閉它。我試過以下內容:功能運行時顯示「請稍候」模式

function flashFallback(){ 
    $('#dialog').dialog({ 
    modal:true, 
    autoOpen:false 
    }); 
    $("#dialog").dialog("open"); 

    /* Other code goes here... 

    */ 

    $('#dialog').dialog("destroy"); 
} 

我知道函數成功運行,但對話框從不關閉。我也試過「關閉」而不是「摧毀」,但沒有運氣。幫幫我!

更新:下面是完整的功能:

function flashFallback(){ 
    $('#dialog').dialog({ 
     modal:true, 
     autoOpen:false 
    }); 
    $("#dialog").dialog("open"); 

    var el = ""; 
    var vidFileName = ""; 
    var posterPath = ""; 
    var videoTag = ""; 
    var boxId = ""; 
    var so = ""; 
    var flashId = ""; 
    var flashVars = ""; 
    var params = ""; 
    var attributes = ""; 
    var anchorId = ""; 
    var dotPosition = ""; 

    $("[id^='vid-']").each(function(){ 
     el = ""; 
     vidFileName = ""; 
     posterPath = ""; 
     videoTag = ""; 
     flashId = ""; 
     flashVars = ""; 
     params = ""; 
     attributes = ""; 
     anchorId = ""; 

     el = $(this); 

     boxId = this.id + "_flashBox"; 
     flashId = this.id + "_flashPlayer"; 
     anchorId = this.id + "_anchor"; 

     el.parent().parent().find("div:first").attr("id",boxId); 

     el.parent().find("source").each(function(){ 
      if ($(this).attr("src").indexOf("m4v") != -1 || 
       $(this).attr("src").indexOf("mp4") != -1){ 
       vidFileName = $(this).attr("src").substring($(this).attr("src").lastIndexOf("/")+1); 
      } 
     }); 

     /* 
      IE uses the Flash player, which overlays a 'Play' button 
      on the poster image by default; so we use a poster image 
      that doesn't have a play button. Otherwise we'd end up 
      with a play button on top of a play button. 
     */ 

     dotPosition = el.parent().find("img").attr("src").lastIndexOf("."); 
     posterPath = el.parent().find("img").attr("src").substring(0,dotPosition) + "-ie" + el.parent().find("img").attr("src").substring(dotPosition); 

     el = $("[id="+boxId+"]"); 
     el.empty(); 
     el.append("<a id='" + anchorId +"'></a>"); 

     flashvars = 
     { 
      file:     vidFileName, 
      autostart:   'false', 
      image:   posterPath 
     }; 

     params = 
     { 
      allowfullscreen:  'true', 
      allowscriptaccess: 'always', 
      wmode:   'opaque', 

     }; 

     attributes = 
     { 
      id:     flashId, 
      name:     flashId 
     }; 

     swfobject.embedSWF('global/vid/player-licensed.swf', anchorId, '372', '209', '9.0.124', 'global/js/swfobject/expressInstall.swf', flashvars, params, attributes); 

    }); 
    $('#dialog').dialog("destroy"); 

} 
+1

模式只能顯示一定的時間嗎? – Marko 2010-07-14 19:30:47

+0

嗨,馬可。是的,只有在上述功能完成時才顯示。 – Alex 2010-07-14 20:46:26

回答

4

JavaScript是單線程的,因此該函數連續運行。它會打開對話框(但不會刷新頁面),然後運行您的代碼,然後在重新繪製網頁之前關閉對話框。

您需要做的是打開對話框並異步運行該功能,完成後要關閉對話框。

+0

謝謝,psycotik。你如何在JavaScript中進行異步調用? – Alex 2010-07-14 19:44:25

+1

@Alex - 'setTimeout(myFunc,0)',通常。 – 2010-07-14 20:23:12

+1

對,顯示你的對話框,設置一個超時,運行你的*真實代碼* 100微秒後,退出該功能。然後,一旦你的計算/等完成,關閉對話框。如果你需要代碼的話。如果你正在進行Ajax呼叫,請執行Mark所說的話。 – psychotik 2010-07-14 20:50:08

1

我不確定你的函數應該做什麼,或者爲什麼需要一個'Please Wait'模式需要足夠長的時間,所以我會假設它的某種請求(AJAX,圖像加載,等等)。

如果是這樣的話,破壞需要在你的函數的回調的一部分內。

+0

嗯。沒有想到這一點。謝謝。 – Alex 2010-07-14 19:44:45

相關問題