簡體   English   中英

javascript,如果else語句更新var

[英]javascript if else statement updating a var

我正在做一個計時器,需要倒計時到下一次在學校上課時。 我用即將到來的演講的日期和一個計時器進行了排列,該計時器正在倒數到排列中的第一個日期。

問題是這樣的:如果計時器已降至0以下,我希望它轉到數組中的下一個日期並遞減到該日期。 現在,我做了一個if語句,像這樣:

if (seconds < 0 ) {
        firstLecture = i++;
        console.log(i)
    }

我可以在控制台中看到我每秒更新一次,但我的計時器在第一次約會時仍在繼續。 我該如何解決這個問題? 我需要循環或類似的東西嗎?

var lectures = ["April 16, 2015 16:25:00", "April 16, 2015 19:25:00", "April 17, 2015 10:10:00", "April 17, 2015 14:20:00"]
var i = 0
var firstLecture = lectures[i];

var date = new Date(firstLecture);
var $display = $('#countdown');
countdown($display, date);
setInterval(function () { countdown($display, date); }, 1000);


function countdown($display, collision) {
    var now = new Date();
    now.setHours(now.getHours() + (get_time_zone_offset()+2));
    var seconds = Math.floor((collision.getTime() - now.getTime()) * 0.001);
    var minutes = (Math.floor(seconds/60) % 60); //60 minutes in an hour
    var hours = (Math.floor(seconds/60/60 * 10)/10) % 24; //24 hours in a day
    var days = Math.floor((seconds/60/60/24));
    //var weeks = Math.ceil((days/7) * 100)/100;
    $display
            .html
    (
            '<p>' +
            collision +
            '<br>seconds: ' + seconds +
            '<br>minutes: ' + minutes +
            '<br>hours: ' + hours +
            '<br>days: ' + days +
                //'<br>weeks: ' + weeks +
            '</p>'
    );

    if (seconds < 0 ) {
        firstLecture = i++;
        console.log(i)
    }
}

function get_time_zone_offset() {
    var current_date = new Date();
    var gmt_offset = current_date.getTimezoneOffset( ) / 60;
    return gmt_offset;
}

在沒有完成整個過程的情況下,您需要執行以下操作:

firstLecture = i++;

看起來應該是:

firstLecture = lectures[++i];

現在,當我相信要將其設置為數組中的下一項時,您將其設置為整數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM