...

View Full Version : function won't stop being called



transmoderata
06-18-2012, 08:09 PM
function inPrevPage() {
inPrevPageCount();
resetIterPrevPage();
}
function inPrevPageCount() {
if ((iterInPrevPage>=0)&&(iterInPrevPage<40)) {
iterInPrevPage++;
inPrevPageMove();
}
}
function resetIterPrevPage() {
iterInPrevPage=0;
}
function inPrevPageMove() {
var jPrevPage=document.getElementById("prevpage");
var jPrevPage_left=jPrevPage.style.left==""?-117:parseFloat(jPrevPage.style.left);
if (jPrevPage_left<26) {
jPrevPage_left=jPrevPage_left+speedPxPrevPage;
jPrevPage.style.left=jPrevPage_left+"px";
setTimeout(inPrevPage,16);
}
else if ((jPrevPage_left>=26)&&(jPrevPage_left<96)) {
speedPxPrevPage=speedPxPrevPage/1.25;
jPrevPage_left=jPrevPage_left+speedPxPrevPage;
jPrevPage.style.left=jPrevPage_left+"px";
setTimeout(inPrevPage,16);
}
}

Simply put, I'm trying to make inPrevPage() execute 40 times. The variable iterPrevPage needs to reset to 0 once inPrevPage has executed 40 times, so that it can do so again in the future.

If I add an alert here:


else if ((jPrevPage_left>=26)&&(jPrevPage_left<96)) {
speedPxPrevPage=speedPxPrevPage/1.25;
jPrevPage_left=jPrevPage_left+speedPxPrevPage;
jPrevPage.style.left=jPrevPage_left+"px";
setTimeout(inPrevPage,16);
alert("inPrevPage() called");
}
}

...the alert keeps on displaying indefinitely. Can someone tell me what's wrong with my code?

WolfShade
06-18-2012, 08:13 PM
Every time you call inPrevPage() you reset the counter to 0 so it will never reach 40.

transmoderata
06-18-2012, 08:25 PM
I was of the idea that inPrevPageCount() would finish executing, THEN resetIterPrevPage() would be called. And debugging with Firebug, I see that iterInPrevPage is reaching 40. So I don't think this is the problem.

transmoderata
06-18-2012, 08:29 PM
I've written the code in a slightly different way to see what it does:


function inPrevPage() {
inPrevPageMove();
resetIterInPrevPage();
}
function resetIterInPrevPage() {
iterInPrevPage=0;
}
function inPrevPageMove() {
var jPrevPage=document.getElementById("prevpage");
var jPrevPage_left=jPrevPage.style.left==""?-117:parseFloat(jPrevPage.style.left);
if ((jPrevPage_left<26)&&(iterInPrevPage>=0)&&(iterInPrevPage<40)) {
jPrevPage_left=jPrevPage_left+speedPxPrevPage;
jPrevPage.style.left=jPrevPage_left+"px";
iterInPrevPage++;
setTimeout(inPrevPageMove,16);
}
else if ((jPrevPage_left>=26)&&(jPrevPage_left<96)&&(iterInPrevPage>=0)&&(iterInPrevPage<40)) {
speedPxPrevPage=speedPxPrevPage/1.25;
jPrevPage_left=jPrevPage_left+speedPxPrevPage;
jPrevPage.style.left=jPrevPage_left+"px";
iterInPrevPage++;
setTimeout(inPrevPageMove,16);
}
}

(note that the function previously named resetIterPrevPage() is now named resetIterInPrevPage() )

Now in Firebug I can see that when inPrevPage() is called, iterInPrevPage reaches 40 but doesn't reset.

???

transmoderata
06-18-2012, 09:43 PM
If found the problem. I wasn't resetting speedPxPrevPage and speedPxNextPage to 15 after each run-though. The functions were calling, the images were just moving extremely slow.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum