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 ifatelse (current.getTime() > expiry2.getTime()) {UPDATE 1
Close each div with
</div>. Fiddle is also updatedUPDATE 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 theelseof the first condition(current.getTime()>expiry.getTime()). Change theelse iftoif, it should work then, although to further optimize the code, you could enclose the secondifwithin the firstif(considering thatexpiryis always less thanexpiry2)