(jQuery) Show and hide div when expiry date/time is reached
<div class="info">
TE开发者_运维百科XT still showing
</div>
<div class="timeout">
TEXT (with display:none)
</div>
When 10:00pm 8.Apr "info" will hide and "timeout"s text will show
GMT +1
Many thanks
something like this would work:
window.setInterval(function(){
var current = new Date();
var expiry = new Date("April 8, 2011 10:00:00")
if(current.getTime()>expiry.getTime()){
$('#timeout').show();
$('#info').hide();
}
}, 5000);
I'm not familiar with javascript date functions so I googled this and used a mixture of w3c and this article to generate this answer
you need to use setInterval to check every second what time it is. Remember it will use client side time. Better way is to use server time to get the duration of elapsed time and set that duration in setInterval to show hide the div.
what you need is two things:
- The code to toogle a visibility properties
- To compare the current time and the target time
var targetTime = new Date(2011, 4, 8, 22, 0, 0);//22 means 22:00 ant that is 10 p.m (12 hours few)
var currentTime = new Date();
var targetMilliSeconds = targetTime.getTime();
var currentMilliSeconds = currentTime.getTime();
if ((currentMilliSeconds - targetMilliSeconds) == 0) { /what ever you want to do/ }
精彩评论