-1

我正在将我当前的日期和时间与我拥有的日期和时间列表进行比较。在代码的最后,我正在比较 getDates 是否大于输出。我想在它为真时停止循环并使用匹配的值(停止我使用返回假)。请问我该怎么做?我做了一个注释,上面写着“匹配值到这里”,我认为这是我需要插入一些东西的地方。

    // Get current date, format it
var d = new Date();

var month = d.getMonth()+1;
var day = d.getDate();
var hour = d.getHours();
var minute = d.getMinutes();

var output = d.getFullYear() + '-' +
((''+month).length<2 ? '0' : '') + month + '-' +
((''+day).length<2 ? '0' : '') + day + ' ' +
((''+hour).length<2 ? '0' :'') + hour + ':' +
((''+minute).length<2 ? '0' :'') + minute
alert(output)

// Get all dates and time found in the table on this page
$('td:first-child').each(function() {
    var getDates = $(this).text();


// Check if dates in table expired. If expired skip and display upcoming one
// if getDates is bigger than output then stop looping and use it

    if ( getDates > output) {
        alert('true')
        return false;
        $('#defaultCountdown').countdown({until: new Date(MATCHED VALUE GOES HERE)});
    }
4

2 回答 2

0

You were returning before the countdown was set.

Try

if ( getDates > output) {
    alert('true')
    $('#defaultCountdown').countdown({until: new Date(getDates)});
    return false;
}
于 2013-09-17T07:11:30.080 回答
0

Make the function return false.

$('td:first-child').each(function() {
    var getDates = $(this).text();


// Check if dates in table expired. If expired skip and display upcoming one
// if getDates is bigger than output then stop looping and use it

    if ( getDates > output) {
        alert('true')
        $('#defaultCountdown').countdown({until: new Date(MATCHED VALUE GOES HERE)});
        return false;
    }
}

From the docs:

We can break the $.each() loop at a particular iteration by making the callback function > return false. Returning non-false is the same as a continue statement in a for loop; it > will skip immediately to the next iteration.

于 2013-09-17T07:11:34.563 回答