/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] setInterval sometimes stacks events and then executes them alltogether :-(

I am using setInterval() to call a function that slides an image slider using jQuery’s animate() method. When I go to another tab or another program for a couple seconds/minutes and then get back to my website, the slider slides for all the events that happened in the meantime, one after another with no pause.

Did anybody EVER have this problem anywhere and how can i possibly solve it? I am using the same “way” of building content sliders with jQuery animations for like a 3 or 4 years now, it never happened before, i am trying to compare my code with previous ones and can see no mistake… i thought it might be the return values, so i tried returning both true and false, changes nothing.

The problem occurs in both Firefox and Chrome (IE not tested).

I don’t know if anyone would wanna read the code, it’s kinda long-ish and messy, but knock yourself out if you wanna

[code=html]// increases faderimage counter and calls faderTo()
function fadeHomepageFader() {
if(!(autoFading)) return true;
currentFaderImage++;
if(!(faderImages[currentFaderImage])) currentFaderImage = 0;
faderTo(currentFaderImage);

return true;
}

// fades current image into selected image, updates links
function faderTo(id) {

// temporary
var tempFading = autoFading;
autoFading=false;

// Just in case faderTo was called directly, next auto fader should know
// which image is currently on
currentFaderImage=id;

// Fade image and image info
$(‘#fader>span’).animate({‘bottom’:’-38px’}, 400, function(){
$(‘#fader>img’).fadeOut(600, ‘easeOutExpo’, function(){
$(‘#fader>span’).html(faderInfos[id]).animate({‘bottom’:’0px’}, 700);
$(‘#fader>img’).attr(‘src’, faderImages[id]).fadeIn(700);
});
});

// Update image-links
$(‘#faderPages>a’).each(function(){
if($(this).attr(‘id’)==’fader-link_’+id) {
$(this).addClass(‘current’);
} else {
$(this).removeClass(‘current’);
}
});

// temporary false not needed anymore
autoFading=tempFading;
}

setInterval(“fadeHomepageFader()”, 6000);
[/code]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@JunkMaleOct 26.2011 — I would say that the timer issue is a JQuery problem.

Also... Rather than tracking an array, rotate its content...

[CODE]function getNextImage(){
if(!faderImages.length) return;
tmp = faderImages.shift();
faderImages.push( tmp );
return tmp;
}[/CODE]


then you do not need to check for an end of array as no end will come!
Copy linkTweet thisAlerts:
@3NexauthorOct 26.2011 — I would say that the timer issue is a JQuery problem.[/QUOTE]
In what sense? As in, there's a bug in (certain versions of) jQuery? Or mixing setInterval() with jQuery code?

But how are those problems avoided? They obviously can be, because i have some sliders which work without this problem.

Also... Rather than tracking an array, rotate its content...

then you do not need to check for an end of array as no end will come![/QUOTE]

Oh sweet, thanks!
Copy linkTweet thisAlerts:
@3NexauthorOct 26.2011 — [SIZE="7"]NEVER MIND[/SIZE]

I found my answer. For anyone else having this problem, the explanation in here:

http://www.google.com/support/forum/p/Chrome/thread?tid=007f9150e46877c2&hl=en
×

Success!

Help @3Nex spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...