943,931 Members | Top Members by Rank

Ad:
Feb 24th, 2007
0

Dynamic Resize DIV

Expand Post »
I have the code bellow and for some reason I have a infinite loop, but I just can't find it to fix it. I just call the function move to dynamicly resize a DIV.

var pos;
var finalPosition;
var myTimer;
var myVar;
var cnt;

function move(finalSize){
cnt=document.getElementById('contentBorder');
pos = cnt.clientHeight;
myVar = cnt.clientHeight;
finalPosition = finalSize;
resize_element(finalPosition);
}

function resize_element(size) {
if (myVar < size){
myTimer=setInterval("increaseBorder()",100);
} else if (myVar > size) {
myTimer=setInterval("decreaseBorder()",100);
} else if (myVar == size) {
} else {
alert('error from: ' + myVar + ' to: ' + size);
}
}

function increaseBorder(){
pos++;

if( pos < finalPosition ){
cnt.style.height = pos + 'px';
} else if(pos >= finalPosition){
clearInterval(myTimer);
} else {
clearInterval(myTimer);
alert('error increase');
}
}

function decreaseBorder(){
pos--;
if( pos > finalPosition ){
cnt.style.height = pos + 'px';
} else if(pos <= finalPosition){
clearInterval(myTimer);
} else {
clearInterval(myTimer);
alert('error increase');
}
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dcarrillo18 is offline Offline
6 posts
since Jul 2006
Feb 26th, 2007
0

Re: Dynamic Resize DIV


Apologies, I didn't read your code thoroughly before posting. Ignore everything up to the first EDIT: note

HTML and CSS Syntax (Toggle Plain Text)
  1. ignore..

This code is checking a global variable called 'myVar'

These two functions are modifying a global variable called 'pos'

HTML and CSS Syntax (Toggle Plain Text)
  1. ignore..

Although you set them both to be the same value in this block...

HTML and CSS Syntax (Toggle Plain Text)
  1. ignore..

...they are values not references; so they wont change simultaneously.

Use the same variable in both functions, and it shouldn't go into an infinate repetition

EDIT: Actually, perhaps that's not the problem, it looks like it all should work... O_o can you find out where the loop is occuring (add some alerts or similar notifications inside the timer functions)

EDIT2: The only situation I can see where a problem could occur would be where you call move(x), then call move(y) without waiting for the first move() to finish completely: in that case, the timer myTimer will be overrwritten, and you'll lose the ability to stop it with clearInterval(). You could avoid that by always calling clearInterval(myTimer) before creating a new interval.
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006
Feb 26th, 2007
0

Re: Dynamic Resize DIV

Hi dcarrillo18.

I made a few edits to this post this morning; they seem to have been forgotton by the system.

The problem is most likely caused when you try to call move() without waiting for a previous move() to finish. That will cause the existing timer ID to be overwritten with a new timer ID, rendering the old timer unreferencable.

The advice I put initially may help you, because checking the current pos value before starting a new timer might block timers from ever being started; but it also might make your code stop functioning after a certain condition is met...

However, I wouldn't rely on it helping solve the overwriting problem. The edit I posted earlier suggested you clear the existing myTimer before creating a new myTimer. I would certainly do that.

Something like this:
HTML and CSS Syntax (Toggle Plain Text)
  1. function move(finalSize){
  2. cnt=document.getElementById('contentBorder');
  3. pos = cnt.clientHeight;
  4. myVar = cnt.clientHeight;
  5. finalPosition = finalSize;
  6. clearInterval(myTimer);
  7. resize_element(finalPosition);
  8. }

If you saw the edit; ignore this post aswell as what I suggested you ignore in the original post. Otherwise, just ignore my original post >,<
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006
Feb 26th, 2007
0

Re: Dynamic Resize DIV

Thank you very much, that seems to solve my problem.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dcarrillo18 is offline Offline
6 posts
since Jul 2006
Feb 26th, 2007
0

Re: Dynamic Resize DIV

That's good, I'm glad you've got it working; despite any confusion I may have caused there. =)
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in HTML and CSS Forum Timeline: Counting hits on a page
Next Thread in HTML and CSS Forum Timeline: Mac bar





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC