在javascript中进行回发之前的延迟

时间:2012-08-01 17:51:14

标签: javascript

现在我能想到在3秒延迟时做回发的唯一方法就是这样。

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

我遇到的问题是它每3秒发回一次。有没有更好的方法来做到这一点或一种方法来清除间隔?我正在使用asp updatepanel控件。

这是我完整的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)
    }

1 个答案:

答案 0 :(得分:2)

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

只会运行一次。

或者您可以使用setInterval的返回值作为间隔线程的引用,然后可以将其清除;

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

编辑:正确,根据评论添加了函数语法。