Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show/Hide divs based on date

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?

like image 983
h0rhay Avatar asked Oct 29 '12 12:10

h0rhay


2 Answers

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 )

like image 152
Jacob George Avatar answered Sep 20 '22 10:09

Jacob George


You forgot about if after else:

else if (current.getTime() > expiry2.getTime()) {
like image 32
VisioN Avatar answered Sep 19 '22 10:09

VisioN