SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Apr 2010
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    jQuery click() every xxx seconds

    I'm trying to trigger a click() function at a set interval.

    I have it working apart from the fact that when I minimise the window the clicks "build up". When the window is opened again all of the clicks are triggered in quick succession.

    I have spent a few hours on this and have come to understand it's to do with jQuery's animation function and that the way around it is the use queue() to stop the clicks going off a set time apart.

    I'm fairly new to jQuery and I don't really understand how to do this though, nothing I try seems to work how I expect. Here is the code i'm using (before attempting to use queue()):

    Code JavaScript:
              var count = 1;
     
    	  timer = window.setInterval(autoAdvance, 7000);
     
              function autoAdvance(){		
     
                    count = count + 1;				
     
                    // Simulating a click on the next slide.
    		$jQ('.image_thumb ul li.slide-' + count).click();		
     
    		if(count == slidesCount)
    		{
    			count = 0; // Goes back to the first slide after reaching the last one.
    		}
     
    	});

    It just simulates a click on a thumbnail, which changes my slider to the next image. slidesCount is a dynamic variable that comes from PHP, it contains a count of how many slides are in the slider.

    Does anyone know how I can solve this? Thank you.

  2. #2
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    In which browser is this happening?
    I've heard reports of Chrome trying to catch up with stuff after it's been maximised again, and I'm not sure if there's a simple solution.

    One thing you could do is to keep a counter in the click function on the slides, sort of a queue in that function.
    If there's already a click in the queue, then don't add another. If there's no click in the queue, then add a new one.
    Not sure if that'll work though, depends a lot on what happens in the click function.

  3. #3
    SitePoint Member
    Join Date
    Apr 2010
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It happens in Firefox and Chrome, I haven't tested it in others yet. It must be possible somehow because there are 100's of jQuery sliders out there which auto-rotate, and I've never run into this problem with any of them, i'm guessing it's just the way i'm doing it.

  4. #4
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    On thing you could try is to use window.setTimeout() instead of setInterval() - simply set a new timeout after every animation.
    My reasoning here is that if stuff is paused while the window is minimized, perhaps the timeout will be paused too...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •