Reputation: 23
I'm setting up a pop-up that I want to show on website every friday and hide it after 24 hours till next friday. Any idea?
Here is my code and fiddle:
var elem = document.querySelector('.elem');
var current = new Date();
var expiry = new Date("April 26, 2019 08:00:00");
if ( current.getTime() >= expiry.getTime() )
{
elem.style.display = "none";
}
<div class="elem">
<h1>Lorem ipsum</h1>
</div>
Upvotes: 0
Views: 443
Reputation: 1528
You would need to check every so often, using a setInterval
:
setInterval(checkTime, 60000);
function checkTime() {
var elem = document.querySelector('.elem');
var current = new Date();
if ( current.getDay() != 5 && elem.style.display == "block" )
{
elem.style.display = "none";
}
else if ( current.getDay() == 5 && elem.style.display == "none" )
{
elem.style.display = "block";
}
}
The second parameter is milliseconds between each check. I've used 60000
, which is one minute. Here are some common times:
Minutes | Milliseconds
--------|-------------
1:00 | 60000
2:00 | 120000
5:00 | 300000
10:00 | 600000
Upvotes: 0
Reputation: 1459
You can check week day with function getDay()
0 for Sunday, 1 for Monday... 5 for Friday
var elem = document.querySelector('.elem');
var current = new Date();
var currentDay = current.getDay()
if (currentDay != 5 ) {
elem.style.display = "none";
}
<div class="elem">
<h1>Lorem ipsum</h1>
</div>
Upvotes: 4