Closed Thread Icon

Topic awaiting preservation: Clearing intervals problem... (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=8844" title="Pages that link to Topic awaiting preservation: Clearing intervals problem... (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Clearing intervals problem... <span class="small">(Page 1 of 1)</span>\

 
Kirk
Nervous Wreck (II) Inmate

From:
Insane since: Sep 2003

posted posted 09-03-2003 10:06

Hello all, I've written this script to allow me to move objects around, but I'm having problems clearing the intervals...
Can anyone tell me what I am doing wrong?

code:
var moveAr = new Array();

function moveFn(id,speed,xPos,yPos){
el = document.getElementById(id);

if (el.moveIndex == null) {
el.moveIndex = moveAr.length;
}

moveAr[el.moveIndex] = el;
clearInterval(this['moveTimer' + el.moveIndex]);
this['moveTimer' + el.moveIndex] = setInterval('moveRecalFn(' + el.moveIndex + ',' + xPos + ',' + yPos + ',' + speed + ')',60);
}

function moveRecalFn(moveIndex,xPos,yPos,speed){
el = moveAr[moveIndex];
if(parseInt(el.style.left) >= (xPos - 1)){
clearInterval(this['moveTimer' + moveIndex]);
el.style.left = xPos + 'px';
}else{
if(parseInt(el.style.left) < xPos){
newPos =+ Math.abs(parseInt(el.style.left) + ((xPos - parseInt(el.style.left)) / speed));
}else if(parseInt(el.style.left) > xPos){
newPos =- Math.abs(parseInt(el.style.left) - ((parseInt(el.style.left) - xPos) / speed));
}

el.style.left = newPos + 'px';
}
}



[This message has been edited by Kirk (edited 09-03-2003).]

[This message has been edited by Kirk (edited 09-03-2003).]

rickindy
Nervous Wreck (II) Inmate

From: Indianapolis, In USA
Insane since: Jan 2002

posted posted 09-04-2003 15:04

There's agood example of attching and unattaching timers to elements at the 1k site.
Take a look at the source and see if helps.
http://www.dithered.com/experiments/1kdhtml/extensions/wm_glide_demo.html



Few problems in life can't be solved by chocolate

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 09-04-2003 19:21

I believe your issues are with your use of the 'this' statement. You are setting those variables to a local scope to each time the function is called as opposed to a constant array of intervals





.:[ Never resist a perfect moment ]:.

Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 09-07-2003 05:26

how does that work in Javascript?

global this['moveTimer' + el.moveIndex] = ...

?


Code - CGI - links - DHTML - Javascript - Perl - programming - Magic - http://www.twistedport.com
ICQ: 67751342

Kirk
Nervous Wreck (II) Inmate

From:
Insane since: Sep 2003

posted posted 09-15-2003 09:41

Thanks for your help guys... here's the finished movement script... use it as you like.

code:
//--------------------------------------------------
// Movement function v0.01 Kirk Bentley
//--------------------------------------------------
function moveFn(id,speed,xPos,yPos,fn){
el = document.getElementById(id);
if (el.moveIndex == null) { el.moveIndex = moveAr.length; }
moveAr[el.moveIndex] = el;
speed = '0.' + speed;
clearTimeout(el.moveTimer);
window.setTimeout('moveRecalFn(' + el.moveIndex + ',' + speed + ',' + xPos + ',' + yPos + ',' + fn + ')',0);
}
function moveRecalFn(moveIndex,speed,xPos,yPos,fn){
el = moveAr[moveIndex];
var moveFloatDisX = Math.round(xPos - parseInt(el.currentStyle.left));
if(parseInt(el.currentStyle.left) < xPos){ floatVelX = Math.ceil(moveFloatDisX * speed); }else{ floatVelX = Math.floor(moveFloatDisX * speed); }
el.style.left = Math.ceil(parseInt(el.currentStyle.left) + floatVelX) + 'px';
var moveFloatDisY = Math.round(yPos - parseInt(el.currentStyle.top));
if(parseInt(el.currentStyle.top) < yPos){ floatVelY = Math.ceil(moveFloatDisY * speed); }else{ floatVelY = Math.floor(moveFloatDisY * speed); }
el.style.top = Math.ceil(parseInt(el.currentStyle.top) + floatVelY) + 'px';
if(document.all){
el.moveTimer = window.setTimeout('moveRecalFn(' + el.moveIndex + ',' + speed + ',' + xPos + ',' + yPos + ',' + fn + ')',40);
}else{
el.moveTimer = window.setTimeout('moveRecalFn(' + el.moveIndex + ',' + speed + ',' + xPos + ',' + yPos + ',' + fn + ')',0);
}

if(moveFloatDisX == 0 && moveFloatDisY == 0){
clearTimeout(el.moveTimer);
el.moveTimer = null;
if(fn != 0){
fn();
}
}
}



Cheers Kirk

« BackwardsOnwards »

Show Forum Drop Down Menu