2013-01-24 47 views
0

我用Google搜索了&我嘗試了幾個答案,但我的問題仍在發生。我在IIS 7上有一個.net網頁,代碼在IE9中正常工作,但在Firefox中不起作用。我需要它在兩個工作。不知道我做錯了什麼,請幫忙。javascript settimeout div not showing

這是一個簡單的頁面有一個多行文本框,當我點擊提交一個隱藏的DIV應該彈出並說,請稍等,而這個過程...不要點擊任何東西(他們可能會提交1000個項目,所以它需要時間。)

有什麼奇怪的是我注意到,如果我取消註釋警報按鈕,警報彈出,我實際上看到的DIV,但如果我拿走警報按鈕,我沒有看到它。

這裏是我的javascript。

function do_totals1() 
    { 
     // alert("Here-1"); 
     document.getElementById('pleasewaitScreen').style.display = 'block'; 

     // alert("Here-2!"); 
     do_totals2(); 

     window.setTimeout(function() { 
     "do_totals2()"; 
     }, 4000); 
     //    
     // alert("Here-3!"); 
     // window.setTimeout("do_totals2()", 1); 
    } 

function do_totals2() 
    { 
     alert("Here-4!"); 
     wait(4000); 
     document.getElementById('pleasewaitScreen').style.display = 'none'; 
    } 



<div id="pleasewaitScreen" style="display:none;position:absolute;z-index:5;top:50%;left:5%;"> 
<table bgcolor="#000000" border="1" style="border-color:blue; height:300;width:400" cellpadding="0" cellspacing="0"> 
    <tr> 
     <td style="width:100%;height:100%" bgcolor="#CCCCCC" align="center" valign="middle"> 
     <br /><br /> 
     <font face="Helvetica,Verdana,Arial" size="5" color="#000066"><b>Processing... Don't click on anything until ESN(s) populate the grid below.</b></font> 
      <br /><br /> 
     </td> 
    </tr> 
</table> 
</div> 
+1

「不工作」對我們沒有任何意義。你期望發生什麼?究竟發生了什麼?你讀過[faq](http://stackoverflow.com/faq)嗎? – Shmiddty

+0

當他們提交表單時會發生什麼?是否調用了do_totals1函數?提供更多信息。它不完整。 – Rocky

+1

這不是問題的根源,而是''do_totals2()「;'不應該用引號引起來。更重要的是,你很明顯沒有向我們展示一些東西,因爲代碼中沒有循環。就像現在一樣,你的代碼顯示'div'然後立即隱藏它。 –

回答

1

這可能是你問的:

function do_totals1() { 
    document.getElementById('pleasewaitScreen').style.display = 'block'; 
    // we don't want to call do_totals2 directly, because it is what hides the element. 
    window.setTimeout(do_totals2, 4000); // setTimeout accepts a function reference 
} 

function do_totals2() { 
    // if wait is effectively a thread sleep function, you don't want to use it. 
    // if javascript doesn't release control of the thread (there's only one for your browser tab), 
    //the UI will never be updated, and you won't see that the element was shown. 
    document.getElementById('pleasewaitScreen').style.display = 'none'; 
} 

如果你正在做的是表示一個單元然後是4秒後隱藏它,你可以這樣做,而不是:

function do_totalsN(){ 
    var ele = document.getElementById('pleasewaitScreen'); 
    ele.style.display = 'block'; 
    setTimeout(function(){ 
     ele.style.display = 'none'; 
    }, 4000); 
} 
+0

謝謝Shmiddty,這在Firefox和IE中適用於我。 –

0

如果你只是想顯示「請稍候,這個過程...」,然後清除它4秒後,這將做

function do_totals1() 
{ 
    document.getElementById('pleasewaitScreen').style.display = 'block'; 
    setTimeout(function() { 
     document.getElementById('pleasewaitScreen').style.display = 'none'; 
    }, 4000); 
} 
+0

這個看起來和shmiddty很相似,所以它應該可以工作。 –

-1

好了,基本上你不叫這種方式你要麼做到這一點:

window.setTimeout("do_totals2();",4000); 

window.setTimeout(function(){ 
    do_totals2(); 
},4000); 

所以基本上,刪除引號,你將被罰款

這裏是更多信息

http://www.w3schools.com/jsref/met_win_settimeout.asp

+1

不是downvoter,但與w3schools指出的相反,你不應該把'setTimeout'表達式放在引號中。使用匿名函數:'window.setTimeout(function(){do_totals2();},4000);' –

+0

完全同意你的觀點,我不會在代碼中放置代碼是因爲評價你必須傳遞一個參數。所以我個人決不會這樣做,但它也是JS的一部分,也是它的文檔。 –