I'm trying to show hide divs based on date and am experimenting with the following code:
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script>
window.setInterval(function(){
var current = new Date();
var expiry = new Date("October 29, 2012 12:00:00")
var expiry2 = new Date("October 30, 2012 12:00:00")
if(current.getTime()>expiry.getTime()){
$('#one').hide();
$('#two').show();
}
else(current.getTime()>expiry2.getTime()){
$('#two').hide();
$('#three').show();
}
}, 3000);
$('#one').show();
</script>
<div id="one" style="display:none">
<p>content for div #one</p>
</div>
<div id="two" style="display:none">
<p>content for div #two</p>
</div>
<div id="three" style="display:none">
<p>content for div three</p>
</div>
Console in chrome is throwing up :
Unexpected token {
So it seems there's a syntax error with the if else statement, but it looks ok to me : (
Can any one spot what I'm missing?
You can check the working jsfiddle if you want
The issue is you require an else if at else (current.getTime() > expiry2.getTime()) {
UPDATE 1
Close each div with </div>. Fiddle is also updated
UPDATE 2
You also have what I think is a logical error. If you want the condition (current.getTime() > expiry2.getTime()) to be activated, it cant come as the else of the first condition (current.getTime()>expiry.getTime()). Change the else if to if, it should work then, although to further optimize the code, you could enclose the second if within the first if (considering that expiry is always less than expiry2 )
You forgot about if after else:
else if (current.getTime() > expiry2.getTime()) {
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With