隐藏Div在一天中的时间

时间:2014-02-17 17:54:31

标签: javascript html

我有一个Javascript倒数计时器,当它达到0时,它会关闭,然后在第二天重置。在它之上我有一个'我们的开放'文本。我想要隐藏该文本,其余部分则是打开'当倒计时达到0时。我遇到麻烦的问题是隐藏div&be;打开'。

Website

Javascript隐藏div&be;打开'

!function ($) {
// get current time
// get current time
var d = new Date(), 
hours = d.getHours(),
mins = d.getMinutes();
day = d.getDay();   

// if day is mon-Fri and time is between 9am and 5:30pm
if(0 < dday < 7   
&& hours >= 21 
&& (hours < 24 || hours === 24 && mins <= 00)){

$('.wereOpen').hide(); 
};
}

倒计时的Javascript:

if (document.getElementById('countdown')) {
pad = function(n, len) { // leading 0's
    var s = n.toString();
    return (new Array( (len - s.length + 1) ).join('0')) + s;
};
function countDown() {
    var now = new Date();
    if ( (now.getDay() >= 1) && (now.getDay() <= 7) ) { // Monday to Sunday
        var target = 21; // 21:00hrs is the cut-off point
        if (now.getHours() < target) { // don't do anything if we're past the cut-off point
            var hrs = (target - 1) - now.getHours();
            if (hrs < 0) hrs = 0;
            var mins = 59 - now.getMinutes();
            if (mins < 0) mins = 0;
            var secs = 59 - now.getSeconds();
            if (secs < 0) secs = 0;
            var str = pad(hrs, 2) + ':' + pad(mins, 2) + '.<small>' + pad(secs, 2) + '</small>';
            document.getElementById('countdown').innerHTML = str;
        }
    }
}
var timerRunning = setInterval('countDown()', 1000);
}

2 个答案:

答案 0 :(得分:1)

我只是用一种完全不同的方式来解决“问题”,我最终得到了以下内容,因为它可能对你有所帮助

var open_times = {
        // day              hh  mm           hh  mm
        Monday:    { open: [ 9, 30], close: [17, 30]},
        Tuesday:   { open: [ 9, 30], close: [17, 30]},
        Wednesday: { open: [ 9, 30], close: [17, 30]},
        Thursday:  { open: [ 9, 30], close: [17, 30]},
        Friday:    { open: [ 9, 30], close: [17, 30]},
        Saturday:  { open: [ 0,  0], close: [ 0,  0]},
        Sunday:    { open: [ 0,  0], close: [ 0,  0]},
    },
    timezone = -(5*60 + 0);

function isOpen() {
    var d = new Date(),
        day, open, close;
    d.setUTCMinutes(d.getUTCMinutes() + timezone);
    day = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][d.getUTCDay()];
    open = open_times[day].open[0] * 3600000 + open_times[day].open[1] * 60000,
    close = open_times[day].close[0] * 3600000 + open_times[day].close[1] * 60000;
    d = +d - (d.setUTCHours(0), d.setUTCMinutes(0), d.setUTCSeconds(0), d);
    if (open <= d && close > d)
        return {open: true, remain: close - d};
    else
        return {open: false, remain: 0};
}

function updateCountDown(ms) {
    var elm = document.getElementById('countdown'),
        ss = pad2(Math.floor(ms / 1000) % 60),
        mm = pad2(Math.floor(ms / 60000) % 60),
        hh = pad2(Math.floor(ms / 3600000) % 24);
    if (elm)
        elm.innerHTML = hh + ':' + mm + '.<small>' + ss + '</small>';
}

function pad2(x) {
    x = '' + x;
    if (x.length === 1) return '0' + x;
    if (x.length === 0) return '00';
    return x;
}

var timerRunning = setInterval(function () {
    var time = isOpen();
    if (time.open) {
        $('.wereOpen').show();
        updateCountDown(time.remain);
    } else
        $('.wereOpen').hide();
}, 1000);

答案 1 :(得分:0)

您的第一段代码中存在一些不正确的变量名称,以及一些逻辑错误。根据你的评论,我调整了if语句:

if((day === 0 || day === 6)
   || (hours < 9 || (hours >= 17 && mins > 30)))
{
    $('.wereOpen').hide();
}

如果当天是星期六或星期日,或者如果时间低于上午9点或高于下午5:30(本地浏览器时间),则此声明将会运行。