Hey folks!
This is my first adventure into the world of JavaScript and I have run into some difficulty.
I am wanting to manipulate some objects on in my document in rudamentary fashion, e.g. move them left and right.
I found out the hard way that a simple for loop with a time delay is not sufficient to do this (appearently the screen is not updated until a function is finished).
Rather, I suppose I am going to have to follow an example I found which makes re-iterative & timed calls to a simple function which does the moving.
I can get all of this to work with a very basic layout (an init routine to set up the action and an actual move routine that moves the object and then calls itself).
However, I would like to make these two routines - well I guess methods on the object in question (to prevent data collision).
So my first question: is this a reasonable decision or is there something I am missing to make this easier?
If I do take this course of action how do I proceed? It looks like I need to extend the document.Element class with my new methods and data or maybe I need to add a new class which is a subclass of the document.element . . .
Well i'm not sure. Here is my sample - two functions which I basically need to make methods of a document element:
//------------------------------------------------------------
var foo = null; // object
function moveMe()
{
foo.style.left = parseInt(foo.style.left)+2+'px'; //increase left position by 2 pixela
if (parseInt(foo.style.left) <= foo.maxright )setTimeout(foo.moveMe(),10); //pause & call again until maxright is reached.
}
function init(arg0)
{
foo = document.getElementById(arg0); // find Element
foo.style.left = (LeftX(foo) - parseInt(foo.style.marginLeft))+'px'; // find initial position and set as an absolute pixel reference
foo.maxright = 500; // set position to stop
moveMe(); //begin loop
}
window.onload = init('LoadPanel01');
//---------------------------------------------------
Thnx for any help
.
This is my first adventure into the world of JavaScript and I have run into some difficulty.
I am wanting to manipulate some objects on in my document in rudamentary fashion, e.g. move them left and right.
I found out the hard way that a simple for loop with a time delay is not sufficient to do this (appearently the screen is not updated until a function is finished).
Rather, I suppose I am going to have to follow an example I found which makes re-iterative & timed calls to a simple function which does the moving.
I can get all of this to work with a very basic layout (an init routine to set up the action and an actual move routine that moves the object and then calls itself).
However, I would like to make these two routines - well I guess methods on the object in question (to prevent data collision).
So my first question: is this a reasonable decision or is there something I am missing to make this easier?
If I do take this course of action how do I proceed? It looks like I need to extend the document.Element class with my new methods and data or maybe I need to add a new class which is a subclass of the document.element . . .
Well i'm not sure. Here is my sample - two functions which I basically need to make methods of a document element:
//------------------------------------------------------------
var foo = null; // object
function moveMe()
{
foo.style.left = parseInt(foo.style.left)+2+'px'; //increase left position by 2 pixela
if (parseInt(foo.style.left) <= foo.maxright )setTimeout(foo.moveMe(),10); //pause & call again until maxright is reached.
}
function init(arg0)
{
foo = document.getElementById(arg0); // find Element
foo.style.left = (LeftX(foo) - parseInt(foo.style.marginLeft))+'px'; // find initial position and set as an absolute pixel reference
foo.maxright = 500; // set position to stop
moveMe(); //begin loop
}
window.onload = init('LoadPanel01');
//---------------------------------------------------
Thnx for any help
.