2012-08-01 116 views
0

我現在認爲在3秒內做回發的唯一方法就是這樣。在javascript中回發之前的延遲

setInterval("__doPostBack('UpdatePanel1', '')", 3000); 

我遇到的問題是它每3秒發回一次。有沒有更好的方法來做到這一點或清除間隔?我正在使用一個asp更新面板控件。

這裏是我完整的javascript代碼給你什麼,我試圖做一個想法:

var delayb4scroll = 1000 //Specify initial delay before marquee starts to scroll on page (2000=2 seconds) 
    var marqueespeed = 2 //Specify marquee scroll speed (larger is faster 1-10) 
    var pauseAtBottom = 3000 //Pause at the end of the scroll for this many seconds (3000 = 3 seconds) 
    var copyspeed = marqueespeed 
    var actualheight = '' 
    var boolRunOnce = '' 

    function pageLoad() { 
     initializemarquee(); 
    } 

    function scrollmarquee() { 
     if (parseInt(cross_marquee.style.top) - 975 > (actualheight * (-1) + 8)) //if scroller hasn't reached the end of its height 
      cross_marquee.style.top = parseInt(cross_marquee.style.top) - copyspeed + "px" //move scroller upwards 
     else { //else, scrollbar has reached the bottom, display for 3 seconds then postback 
      setInterval("__doPostBack('UpdatePanel1', '')", 3000);   
     } 
    } 

    function initializemarquee() { 
     cross_marquee = document.getElementById("vmarquee") 
     cross_marquee.style.top = 0 
     marqueeheight = document.getElementById("marqueecontainer").offsetHeight 
     actualheight = cross_marquee.offsetHeight //height of marquee content (much of which is hidden from view)  
     setTimeout('lefttime=setInterval("scrollmarquee()",30)', delayb4scroll) 
    } 
+2

嘗試setTimeout,它像setInterval但它只運行一次 – hackattack 2012-08-01 17:52:55

+4

注意:不要傳遞字符串到'setTimeout' /'setInterval';它使用'eval'!傳遞函數:'setInterval(function(){__doPostBack('UpdatePanel1','');},3000);'。 – 2012-08-01 17:54:56

回答

2
setTimeout(function() { __doPostBack('UpdatePanel1', ''); }, 3000); 

將只運行一次。

或者您可以使用setInterval的返回值作爲間隔線程的引用,然後可以清除它;

myInterval = setInterval(function() { __doPostBack('UpdatePanel1', ''); }, 3000); 
//later 
clearInterval(myInterval); 

編輯:正確,函數的語法加按的意見。

+0

我做了更改,但似乎是在調試模式下反覆調用updatepanels方法,而不是一次。 – lagwagon223 2012-08-01 18:46:32