May 3, 2012

jquery load between two urls

Question by Mr. 1.0

I am trying to load content from 2 different URLS but at different intervals. Ideally I would like to load one url wait 10 seconds load the other and repeat the over and over. This is what I have but its not working correctly. Its constantly loading the urls rapidly back and forth so fast I cant read the content

setInterval(function(){
          $('#ticker').load('misc.php?users=10');
      }, 10000);

      setInterval(function(){
          $('#ticker').load('misc.php?news=10');
      }, 20000);

Answer by Starx

I have a better suggestion

var links = ['misc.php?users=10', 'misc.php?news=10'];
var pointer = 0;

setInterval(function() {
     $("#ticker").load(links[pointer++ % links.length]);
}, '10000');

Demo

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!