如何使javascript小部件仅在特定时间出现

时间:2015-06-05 23:41:33

标签: javascript time

我正在尝试让javascript小部件仅在早上6点到下午6点之间出现在网站上,这可能吗?

1 个答案:

答案 0 :(得分:0)

好吧,那么,当加载网页时,只有在早上6点到下午6点之间才会显示小部件?理解是的简单逻辑是...... see it workingsee the reference

var nowIs = new Date();
// if you need to compare the seconds or minutes can use nowIs.getMinutes() and nowIs.getSeconds()
// se more in http://www.w3schools.com/jsref/jsref_obj_date.asp
if(nowIs.getHours() >= 6 && nowIs.getHours() <= 18){
    alert("Hey, now is between 6am and 6pm :)");
}else{
    alert("That bad, now is not between 6am and 6pm :(");
}