PDA

View Full Version : Jasoop setTimeout



magicyte
10-22-2008, 03:26 AM
Okay: you may have heard I've been working on a fairly buggy 'toolbox'. One little problem: I tried making a custom setTimeout function. There isn't an error, but I do have one concern. How could I make it NOT pause when the user sets the timeout? I have a do..while loop. I want the thing to do what I want without pausing the screen and the user. Here is the code:


/********** - IMPORTANT LABEL - **********/
/* Title: Jasoop Toolbox */
/* Name: jasoop.js */
/* Author: 2008 Arthur C. Watkins */
/* Notice: This label is to stay intact */
/* for usage of this document. */
/********** - IMPORTANT LABEL - **********/

// Start Jasoop Toolbox

var _ = function (el) {
if(document.getElementById()) return document.getElementById(el);
else if(document.all) return document.all[el];
else if(document.layers) return document.layers[el];
else return false;
};

var Concatenate = function () {
var string = '';
for(x=0;x<arguments.length;x++) {
string=string+arguments[x];
}
return string;
};

var clock = Date.now || function(){
return new Date().getTime();
};

var wait = function (statement,delay) {
var start,end,loop;
start = clock();
end = (start+delay);
do { // my problem
loop = clock(); // my problem
} while (loop<end); // my problem
setTimeout(statement,0);
};

var Animation = function (el) {
this.element = el;
this.x = [];
this.y = [];
this.delay = [];
this.ac = 0;
this.lc = 0;
this.pause = false;
this.autodelete = false;
this.loop = false;
};

Animation.prototype.moveTo = function (x,y) {
_(this.element).style.position="absolute";
_(this.element).style.left=(x+"px");
_(this.element).style.top=(y+"px");
};

Animation.prototype.start = function () {
var animation = this;
if(!this.pause)
{
if(this.ac<this.lc)
{
this.moveTo(this.x[this.ac],this.y[this.ac]);
this.ac++;
setTimeout(function(){animation.start()},this.delay[this.ac]);
}
else if(this.ac>=this.lc)
{
(this.autodelete) ? this.reset() : this.ac=0;
(this.loop && !this.autodelete) ? this.start() : "";
}
}
else if(this.pause)
{
this.pause = !this.pause;
}
};


Animation.prototype.stop = function () {
this.pause = true;
};

Animation.prototype.compute = function (l,t,d) {
this.x[this.lc]=l;
this.y[this.lc]=t;
this.delay[this.lc]=d;
this.lc++;
};


Animation.prototype.reset = function() {
for(i=0;i<=this.ac;i++)
{
this.x[i]=0;
this.y[i]=0;
this.delay[i]=0;
}
this.lc=0;
this.ac=0;
};

// End Jasoop Toolbox

/********** - IMPORTANT LABEL - **********/
/* Title: Jasoop Toolbox */
/* Name: jasoop.js */
/* Author: 2008 Arthur C. Watkins */
/* Notice: This label is to stay intact */
/* for usage of this document. */
/********** - IMPORTANT LABEL - **********/

Please help.

-magicyte

Nile
10-22-2008, 03:46 AM
I don't get what you mean, do you have an example page?

Twey
10-22-2008, 12:51 PM
You'd have to use setTimeout(). Do not use eval().

Additionally, I see that most of the issues I pointed out in your other thread are still there, some weeks later.