0

これが私の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>

基本的に、.countdown要素にテキストの変更がある場合は、タイマーをオンに戻したいと思います。

その値を更新するために使用する AJAX 呼び出しにより、テキストが変更されます。

現在、タイマーは再度有効にならず、.Countdown の値が変更されるとカウントダウンがフリーズします。要素のテキストが変更されたときに change() イベントが発生すると思います。これは正しいです?

私の側に明らかな間違いはありますか?

4

2 に答える 2

0

コードにループと条件が含まれています。

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

これらの要素の 1 つが条件を満たす値を持っている可能性が非常に高いですnewValue <= 1。その場合、タイマーは停止します。入力フィールドの内容を変更しても、タイマーはその実行後にすぐに停止します。

複数のタイマーをサポートする必要がある場合は、タイマーのウォレット (var timers = {};timers.time1=...またはvar timers = [];timers[0] = ...) を使用します。少数のタイムアウトのみをサポートする必要がある場合は、変数 ( var timer1=... ,timer2=..., timer3=...;) を使用することもできます。

于 2011-09-21T21:12:38.200 に答える
0

要素が存在する前に変更イベントをバインドしようとしています。そのコードをreadyイベント ハンドラー内に配置します。

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

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

});

またtimer、タイマーがオフのときに変数を識別可能な値 (例: null) に設定すると、タイマーを開始する前にその値を確認して、複数のタイマーが開始されるのを防ぐことができます。

于 2011-09-21T20:32:27.380 に答える