Javascript 调用clearInterval后,如何重新启用setInterval?

Javascript 调用clearInterval后,如何重新启用setInterval?,javascript,jquery,setinterval,clearinterval,Javascript,Jquery,Setinterval,Clearinterval,以下是我的javascript: <script type="text/javascript"> var timer; $(document).ready(function () { timer = setInterval(updatetimerdisplay, 1000); $('.countdown').change(function () { timer = setInterval(updatetimerdisplay, 1000);

以下是我的javascript:

<script type="text/javascript">
var timer;

$(document).ready(function () {
    timer = setInterval(updatetimerdisplay, 1000);

    $('.countdown').change(function () {
        timer = setInterval(updatetimerdisplay, 1000);
    });

    function updatetimerdisplay() {
        $(".auctiondiv .auctiondivleftcontainer .countdown").each(function () {
            var newValue = parseInt($(this).text(), 10) - 1;
            $(this).text(newValue);

            if (newValue >= 9) {
                $(this).css("color", "");
                $(this).css("color", "#4682b4");
            }

            if (newValue == 8) {
                $(this).css("color", "");
                $(this).css("color", "#f3982e");
            }

            if (newValue == 5) {
                $(this).css("color", "");
                $(this).css("color", "Red");
            }

            if (newValue <= 1) {
                //$(this).parent().fadeOut();
                clearInterval(timer);
            }
        });
    }
});

var updateauctionstimer = setInterval(function () {
    $("#refreshauctionlink").click();
}, 2000);

function updateauctions(response) {
    var data = $.parseJSON(response);

    $(data).each(function () {
        var divId = "#" + this.i;
        if ($(divId + " .auctiondivrightcontainer .latestbidder").text() != this.b) {
            $(divId + " .auctiondivrightcontainer .latestbidder").fadeOut().fadeIn();
            $(divId + " .auctiondivrightcontainer .auctionprice .actualauctionprice").fadeOut().fadeIn();
            $(divId + " .auctiondivleftcontainer .countdown").fadeOut().fadeIn();
        }

        $(divId + " .auctiondivrightcontainer .latestbidder").html(this.b);
        $(divId + " .auctiondivrightcontainer .auctionprice .actualauctionprice").html(this.p);

        if ($(divId + " .auctiondivleftcontainer .countdown").text() < this.t) {
            $(divId + " .auctiondivleftcontainer .countdown").html(this.t);
        }
    });
}
</script>

无功定时器;
$(文档).ready(函数(){
定时器=设置间隔(updatetimerdisplay,1000);
$('.countdown').change(函数(){
定时器=设置间隔(updatetimerdisplay,1000);
});
函数updateTimeDisplay(){
$(.auctiondiv.auctiondivleftcontainer.countdown”).each(函数(){
var newValue=parseInt($(this).text(),10)-1;
$(this).text(newValue);
如果(新值>=9){
$(this.css(“color”,”);
$(this.css(“color”和“#4682b4”);
}
如果(newValue==8){
$(this.css(“color”,”);
$(this.css(“color”,“f3982e”);
}
如果(newValue==5){
$(this.css(“color”,”);
$(this.css(“颜色”、“红色”);
}

如果(newValue您试图在元素存在之前绑定更改事件。请将该代码放入
ready
事件处理程序中:

$(document).ready(function () {
  timer = setInterval(updatetimerdisplay, 1000);

  $('.countdown').change(function () {
    timer = setInterval(updatetimerdisplay, 1000);
  });

});

当计时器关闭时,您可能还希望将
计时器
变量设置为可识别的值(例如
null
),以便在启动计时器之前检查该值,以防止启动多个计时器。

您的代码包含循环和条件:

function updatetimerdisplay() {
    $(".auctiondiv .auctiondivleftcontainer .countdown").each(function () {
    ...
        if (newValue <= 1) {
            //$(this).parent().fadeOut();
            clearInterval(timer);
        }
        ...
    }
}
函数updatetimerdisplay(){
$(.auctiondiv.auctiondivleftcontainer.countdown”).each(函数(){
...

如果(newValue)是一个全局变量
timer
来存储你的计时器ID?这不是一个好主意…@Sime:请告诉我!我对javascript是绿色的。:)这看起来像是@Dave:怎么回事?这要求完全不同。特别是在元素的文本更改后如何重新启动一个死机计时器。:)@Sergio Read here:您不能检查我的编辑吗?尽管.countdown文本已更改,但计时器未重新启用。@Sergio--这是因为运行该代码时.countdown元素不存在。请将其放置在
$(document).ready(function(){
按照Guffa的建议,尝试一下。@Sergio Tapia:change
事件是在用户更改字段中的值并将焦点更改为另一个元素时触发的。它不是通过按代码更改字段的值触发的。@Guffa:Gah!那么您建议我使用什么事件?这就是我遇到的全部问题。:)谢谢您的时间。@Sergio Tapia:您可以在更改值时手动触发事件,但是将代码放入函数并调用它更容易。我刚刚被告知更改事件会触发输入。我想检测
元素中的文本何时更改。我可以使用什么事件?无
更改
事件处理程序只能用于检查输入更改。您可以添加轮询器,并通过以下方式完成类似的结果:让
元素
成为
元素:
var p_lastString=element.innerHTML;window.setInterval(function(){if(element.innerHTML!=p_lastString){p_lastString=element.innerHTML;警报(“字符串已更改!”)}}500);