2

I am looking to hide several divs one by one or with a time interval of 5 seconds, i tried below doesn't seem to work though

<div id="container">
<div id="data1">123</div>
<div id="data2">456</div>
<div id="data3">789</div>
<div id="data4">012</div>
</div>
<script>

$('document').ready(function(){
     window.setTimeout('mytimer()',5000);
  });
$('document').ready(function(){
     window.setTimeout('mytimer2()',10000);
  });

$('document').ready(function(){
     window.setTimeout('mytimer3()',15000);
  });

$('document').ready(function(){
     window.setTimeout('mytimer4()',20000);
  });


  function mytimer(){   $('#data1').hide();    } 
  function mytimer2(){   $('#data2').hide();    } 
  function mytimer3(){   $('#data3').hide();    } 
  function mytimer4(){   $('#data4').hide();    } 
  </script>
0

7 Answers 7

1

I would use single timeout function as your are hiding at regular intervals. There is one mistake in your code you need to pass the reference of function to setTimeout instead of passing the function call as a string.

Live Demo

window.setTimeout(mytimer,1000);
index = 1;
function mytimer()
{     
     $('#data' + (index++)).hide();
     if(index <= 4) window.setTimeout(mytimer,1000);
}
Sign up to request clarification or add additional context in comments.

Comments

0

You need to use $(document) instead of $('document')

$('document') will look for HTML Element with document tag, which doesn't exist.

Learn to use developer tools, Here's a good read: How to open the JavaScript console in different browsers?

Code

$(document).ready(function(){
     window.setTimeout(mytimer,5000); //You can simply pass the function reference 
     window.setTimeout(mytimer2,10000);
     window.setTimeout(mytimer3,15000);
     window.setTimeout(mytimer4,20000);
 });

Comments

0

Try it this way:

$(document).ready(function(){
    window.setTimeout(mytimer, 5000);
    window.setTimeout(mytimer2, 10000);
    window.setTimeout(mytimer3, 15000);
    window.setTimeout(mytimer4, 20000);
});
function mytimer(){
    $('#data1').hide();
}
function mytimer2(){
    $('#data2').hide();
}
function mytimer3(){
    $('#data3').hide();
}
function mytimer4(){
    $('#data4').hide();
}

Comments

0

Well you can use setInterval function too for this and once all the elements have been hidden you can clearInterval like one below:

DEMO HERE

function mytimer(elem){   
    console.log('came here');
    $(elem).hide();    
} 

$(document).ready(function(){
    var i=0;
    var interval=null;
    interval = window.setInterval(function(){
         i++;
         if(i<=$('#container').children().length)
             mytimer("#data"+i);
        else
        {
            clearInterval(interval);
            return;
        }
    },5000);
});

Comments

0

Try this change and so on for the rest:

window.setTimeout(mytimer, 5000);// removed quotes and `()`

Another solution using jQuery fadeOut():

$(function() {
  for (var i = 1; 4 >= i; i++)
    $('#data' + i).fadeOut(5000 * i);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="container">
  <div id="data1">123</div>
  <div id="data2">456</div>
  <div id="data3">789</div>
  <div id="data4">012</div>
</div>

Comments

0

Use .delay() in jquery . No Settimeout function needed.

$('#data1').delay(5000).hide('fast');
$('#data2').delay(10000).hide('fast');
$('#data3').delay(15000).hide('fast');
$('#data4').delay(20000).hide('fast');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
<div id="data1">123</div>
<div id="data2">456</div>
<div id="data3">789</div>
<div id="data4">012</div>
</div>

Comments

0

There are multiple errors in your code.

  1. You have used $('document').ready(function(){}), which is incorrect. document is a keyword, it shouldn't be in quotes.
  2. You don't have to use multiple instance of calling $(document).ready(). You can call all your statements from a single function. You can also use $(function(){}).
  3. While calling the function name inside the timeout function, you shouldn't put them under quotes. They act like keywords after you have defined them in your code. The function call inside the Timeout function shouldn't be followed by (). So it should be window.setTimeout(mytimer,5000);

Please refer the fiddle : http://jsfiddle.net/65gs8s9y/

I have modified your code which works fine now:

$(document).ready(function(){
    window.setTimeout(mytimer,5000);
    window.setTimeout(mytimer2,10000);
    window.setTimeout(mytimer3,15000);
    window.setTimeout(mytimer4,20000);
  });

  function mytimer(){   
      $('#data1').hide();    
  } 
  function mytimer2(){   
      $('#data2').hide();    
  } 
  function mytimer3(){   
      $('#data3').hide();    
  } 
  function mytimer4(){   
      $('#data4').hide();    
  } 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.