Is there a js function that will allow me to show an iframe only during certain hours? For example, only between 7:30pm and 7:00am the next day.
答案 0 :(得分:2)
Using the following methods of the Date Object you should be able to generate the time.
var today = new Date()
var hour = today.getHours()
var minutes = today.getMinutes()
You could call on the date object every minute with setInterval()
to find out if it is between 7:30 PM and 7:00 AM. then you could clear the interval, and set another timer to reload the iframe at 7:00 AM. where you setInterval
again to check every minute
Please Note: getHours()
will return the hour on a 24 hour clock so if it is 7:00 PM today.getHours()
outputs 19
also there is a very useful library called moment.js that you might want to look into for JS problems involving time (seems overkill for just finding 7:30 and 7:00 though) see here
答案 1 :(得分:1)
This question is already answered before. You can find it here link. Anyways just posting the answer.
var startTime = '7:00 AM';
var endTime = '7:30 AM';
var now = new Date();
var startDate = dateObj(startTime); // get date objects
var endDate = dateObj(endTime);
var flag = now < endDate && now > startDate ? 'showFrame' : 'hideFrame'; //
compare
if(flag === 'showFrame')
{
console.log(flag);
// show Frame
}
function dateObj(d) { // date parser ...
var parts = d.split(/:|\s/),
date = new Date();
if (parts.pop().toLowerCase() == 'pm') parts[0] = (+parts[0]) + 12;
date.setHours(+parts.shift());
date.setMinutes(+parts.shift());
return date;
}
Fiddle : Please see this Fiddle