根据当前的日期和时间停止/隐藏JavaScript运行? (jQuery的?) [英] Stop/hide javascript running depending on current day and time? (jquery?)

查看:95
本文介绍了根据当前的日期和时间停止/隐藏JavaScript运行? (jQuery的?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个项目(实时聊天),它使用一些javascript代码将聊天按钮放到网页上。我想要做什么,具体取决于一周中的哪一天以及几点 - 显示或隐藏此javascript代码,但是,只有在实时聊天打开时才会显示该按钮。



我查看了jquery,看起来这很有用,但是,我之前没有用过它,而且在编写代码时遇到了麻烦。



我找到了一个项目( https://jsfiddle.net/16mnrL3b/)这似乎做我需要的基础知识,但我不知道如何每天输入值。我需要每天有不同的开放时间,例如星期一 - 上午9点到下午5点,星期二 - 上午10点 - 下午4点等...

  //获取当前时间。 
var d = new Date(); (d.getHours()> = 7& d.getHours()< = 15){
$(。open)。show();
$(。closed)。hide();
}
else {
$(。closed)。show();
$(。open)。hide();
}

-

 < div class =open>商店已开启< / div> 
< div class =closed>商店已关闭< / div>

任何人都可以帮忙吗?

解决方案

详细了解 Date()

p>

  // 0星期天
// 1星期一
// 2星期二
// .. 。
// 6星期六

var d = new Date();
var openHours = 0;
var closedHours = 0;

switch(d.getDay()){
case 0:
openHours = 7;
closeHours = 17;
休息;
案例1:
openHours = 8;
closeHours = 18;
休息;
案例2:
openHours = 7;
closeHours = 17;
休息;
案例3:
openHours = 8;
closeHours = 18;
案例4:
openHours = 7;
closeHours = 17;
休息;
案例5:
openHours = 7;
closeHours = 17;
休息;
案例6:
openHours = 9;
closeHours = 15;
休息; (d.getHours()> = openHours&& d.getHours()< = closeHours){
$(b


。开)显示();
$(。closed)。hide();
} else {
$(。open)。hide();
$(。closed)。show();

$ / code>

这将根据日期设置小时数并相应显示


I am working on a project (live chat) which uses some javascript code to place a chat button onto a web page. What I'd like to do, depending specifically on which day of the week it is and what time it is - is either display or hide this javascript code, ergo, the button only appears when the live chat is "open".

I have looked into jquery which seems very capable of this, however, I have not used it before and I'm having trouble working out the code.

I found a project (https://jsfiddle.net/16mnrL3b/) which seems to do the basics of what I need, but I have no idea how to input values per day. I'd need each day to have different opening hours, e.g. Monday - 9 am to 5 pm, Tuesday - 10 am - 4 pm etc...

//gets the current time. 
var d = new Date();
if(d.getHours() >= 7 && d.getHours() <= 15 ){
    $(".open").show();
    $(".closed").hide();
}
else {  
     $(".closed").show();
    $(".open").hide();
}

--

<div class = "open">Shop is open</div>
<div class = "closed">Shop is closed</div>

Can anyone help with this?

解决方案

Look more into Date()

// 0 Sunday
// 1 Monday
// 2 Tuesday
// ...
// 6 Saturday

var d = new Date();
var openHours = 0;
var closedHours = 0;

switch(d.getDay()) {
    case 0:
        openHours = 7;
        closeHours = 17;
        break;
    case 1:
        openHours = 8;
        closeHours = 18;
        break;
    case 2:
        openHours = 7;
        closeHours = 17;
        break;
    case 3:
        openHours = 8;
        closeHours = 18;
    case 4:
        openHours = 7;
        closeHours = 17;
        break;
    case 5:
        openHours = 7;
        closeHours = 17;
        break;
    case 6:
        openHours = 9;
        closeHours = 15;
        break;
}


if(d.getHours() >= openHours && d.getHours() <= closeHours) {
    $(".open").show();
    $(".closed").hide();
}else {
    $(".open").hide();
    $(".closed").show();
}

This will set the hours depending on the day and display them accordingly

这篇关于根据当前的日期和时间停止/隐藏JavaScript运行? (jQuery的?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆