-1

我的语法有一些问题 :( 需要在哪里以及如何关闭准备好?这个脚本必须在加载页面后加载。

function wr_hours(item){
//timeout
var sek = item.data("timeout");
var min = Math.floor((sek /60)%60) ;//sec
var hour = Math.floor((sek / (60*60)) %24) ;//hr
var days = Math.floor(sek /(24*60*60)) ;//days
var counterWrapper = $("#b-countdown");

if ( min < 10 ){
    min = "0" + min;
}
//CSS       
if ( hour != counterWrapper.data("left") ){
    var hourClass = hour == 0 ? 1 : hour;
    counterWrapper.attr("class", "left-" + hourClass).attr("data-left", hourClass)
}

var time_wr = hour + ":" + min;
item.html(time_wr);
item.data("timeout", (sek - 1) > 0 ? sek - 1 : 24 * 60 * 60);
}
setInterval(function(){
    $(".deadline").each(function(){ wr_hours($(this));})
}, 1000);
4

2 回答 2

1

你的意思是以下:

$(document).ready(function() {
    function wr_hours(item){
        //timeout
        var sek = item.data("timeout");
        var min = Math.floor((sek /60)%60) ;//sec
        var hour = Math.floor((sek / (60*60)) %24) ;//hr
        var days = Math.floor(sek /(24*60*60)) ;//days
        var counterWrapper = $("#b-countdown");

        if ( min < 10 ){
            min = "0" + min;
        }
        //CSS       
        if ( hour != counterWrapper.data("left") ){
            var hourClass = hour == 0 ? 1 : hour;
            counterWrapper.attr("class", "left-" + hourClass).attr("data-left", hourClass)
        }

        var time_wr = hour + ":" + min;
        item.html(time_wr);
        item.data("timeout", (sek - 1) > 0 ? sek - 1 : 24 * 60 * 60);
    } setInterval(function(){
        $(".deadline").each(function(){ wr_hours($(this));})
    }, 1000);
});

但是,由于您是在函数wr_hours中调用setInterval函数,另一种方法是将setInterval部分放在$(document).ready(function() { ... });

于 2013-11-11T14:14:52.797 回答
1

JQuery 就绪函数可以这样使用:

$(document).ready(function () {
/* ADD YOUR CODE HERE */
});

在您的示例中:

function wr_hours(item){
    //timeout
    var sek = item.data("timeout");
    var min = Math.floor((sek /60)%60) ;//sec
    var hour = Math.floor((sek / (60*60)) %24) ;//hr
    var days = Math.floor(sek /(24*60*60)) ;//days
    var counterWrapper = $("#b-countdown");

    if ( min < 10 ){
        min = "0" + min;
    }
    //CSS       
    if ( hour != counterWrapper.data("left") ){
        var hourClass = hour == 0 ? 1 : hour;
        counterWrapper.attr("class", "left-" + hourClass).attr("data-left", hourClass)
    }

    var time_wr = hour + ":" + min;
    item.html(time_wr);
    item.data("timeout", (sek - 1) > 0 ? sek - 1 : 24 * 60 * 60);
}
$(document).ready(function() {
    setInterval(function(){
        $(".deadline").each(function(){ wr_hours($(this));})
    }, 1000);
});
于 2013-11-11T14:16:41.987 回答