Closed Thread Icon

Preserved Topic: Timeout Functions sending variables (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=17968" title="Pages that link to Preserved Topic: Timeout Functions sending variables (Page 1 of 1)" rel="nofollow" >Preserved Topic: Timeout Functions sending variables <span class="small">(Page 1 of 1)</span>\

 
kars10
Bipolar (III) Inmate

From: Europe
Insane since: Mar 2001

posted posted 05-15-2001 16:16

We all need the Timout functiones for a slide or something.
But why can't it send vaiables to a function?
It works fine like this: setTimeout('sendToFunction()',500);
but it can't do that: setTimout('sendToFunction(variable1,varible2)',500);

This is dissappointing of this function. It would be so easy I it could send variables around.
Now, is it my fault or is it just something else?
k10

Bugimus
Maniac (V) Mad Scientist

From: New California
Insane since: Mar 2000

posted posted 05-15-2001 23:58

kars10,

You *can* send variables using setTimeout()

The way you described probably doesn't work because variable1 and variable2 are executed as strings. It does not take the *value* of these variable names but rather passes "variable1" and "variable2" to your function.

Say variable1=25 and variable2=3. You could do something like:

1) setTimout("sendToFunction("+variable1+","+variable2+")",500);

or

2) setTimout("sendToFunction(25,3)",500);

I hope that helps.


Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 05-16-2001 00:51

Yup, what bug said will work.

The reason setTimeout('myfunction(var1,var2)',1000) doesn't work is because var1 and var2 are defined in the place (we say in the "scope") with the setTimeout function. But the code that the setTimeout function runs is in it's *own* scope. It doesn't have any of the same variables unless they're global (defined outside of any functions). The reason setTimeout('myfunction(' + var1 + ',' + var2 + ')', 1000) works is because the string inside actually contains the *values* of var1 and var2.

kars10
Bipolar (III) Inmate

From: Europe
Insane since: Mar 2001

posted posted 05-16-2001 02:07

hey, that sounds very smart...
...
and it works. Thank you guys. No that I know this stuff I finally can rewrite my api...
thanks
k10

linear
Paranoid (IV) Inmate

From: other places
Insane since: Mar 2001

posted posted 05-16-2001 05:46

Can anyone say KLUGE?

That's a truly ugly workaround.

kars10
Bipolar (III) Inmate

From: Europe
Insane since: Mar 2001

posted posted 05-16-2001 16:19

ok, that does a great job passng numbers, but what about a string?

theObj is a string (actually the id of the object)

function index1(theObj,px,py,pjx,pjy,pex,pey,wait)
{slide(theObj,px,py,pjx,pjy,pex,pey,wait);}

function slide(theObj,px,py,pjx,pjy,pex,pey,wait)
{
if (fSlide){fSlide=false; shiftTo(theObj,px,py); setTimeout('slide('+theObj+','+px+','+py+','+pjx+','+pjy+','+pex+','+pey+','+wait+')',wait);}
else
{
if ((px<=pex)&&(py<=pey))
{px +=pjx; py +=pjy; shiftTo(theObj,px,py); setTimeout('slide('+theObj+','+px+','+py+','+pjx+','+pjy+','+pex+','+pey+','+wait+')',wait);}
else {}
}
}

I placed some alerts everywhere to fid out what happened. it does fine until it gets send by the setTimeoutFunction.
It mutates to the whole slide function-script! am i doing something wrong with the apostrophes?
k10
(and yes, it is a klunge, but if it workes it would save me a lot of space...

ShadowImage
Nervous Wreck (II) Inmate

From: Melbourne, Victoria, Australia
Insane since: Mar 2001

posted posted 05-16-2001 16:49

passing strings?
setTimout("sendToFunction( ----->' <---"+variable1+"','"+variable2+"')",500);

just add the ' thing so it will look like this: setToFunctino('variable1','variable2')
var timeoutstring = "sendToFunction('" + variable1 + "','" + variable2 "')";
setTimeout(timeoutstring, 500);
make sense?

Darkness is only the beginning

kars10
Bipolar (III) Inmate

From: Europe
Insane since: Mar 2001

posted posted 05-16-2001 17:36

good point you got there, ShadowImage, I think I didn't try the first one.

but what are you doing with that:
1.) sendToFunction('variable1','variable2');
2.) var timeoutstring = "sendToFunction(' " + variable1 + " ',' " + variable2 " ')";
3.) setTimeout(timeoutstring, 500);
Just to make sure if I got it right...
1.) it's just the function and what its preferences are...
2.) the new variable is a string containing a normal call to that function(with the variables kept as strings...)
3.) TimeoutFunction refering to the vatiable refering to the function...?
It's either genious or I didn;t get it... Unfortunatley I don;t have the means to check it out at school
k10


kars10
Bipolar (III) Inmate

From: Europe
Insane since: Mar 2001

posted posted 05-18-2001 02:49

as far as i understand it is a giving and taking here in ozones asylim. so now it is my time to give:

this is a function that moves objects around:
it's an api, just suck the information you need out of it...

[hr]


/********************************************************************************************api by Karsten Hiekmann. custom objects ********************************************************************************************/

/*******************************************************************************************
start of api**********************************************************************************/

//ERROR TRAPPING CODE -- this is an error trapping code. doesn't work in NN though....
window.onerror=null;

//browser sniffer. but no Mozilla
function checkb()
{
this.ver = navigator.appVersion
this.ie = (document.all) ? 1:0;
this.n = (document.layers) ? 1:0;
this.ie4 = (this.ver.indexOf('MSIE 4')>0);
this.ie4mac = this.ie4 && navigator.userAgent.indexOf("Mac")>-1;
this.ok = (this.ie &#0124; &#0124; this.n) && (!this.ie4mac);
return this
}
br=new checkb();

//alert(br.ver);alert(br.ie);alert(br.n);alert(br.ie4);alert(br.ie4mac);alert(br.ok); -- I used this one
//to find out about my own 3 browsers(!)


//object constructor. -- makes an object. VERY nice thing. needs some special attention! (*SA)
function makeObj(id,nest)
{
if (br.n)
{
nest=(!nest) ? '':'document.'+nest+'.';
this.css = eval(nest+"document.layers." +id);
this.x = this.css.left;
this.y = this.css.top;
this.w = this.css.document.width
this.h = this.css.document.height;
}
if (br.ie)
{
this.elm = document.all[id];
this.css = document.all[id].style;
this.x = this.elm.offsetLeft;
this.y = this.elm.offsetTop;
this.w = this.elm.offsetWidth;
this.h = this.elm.offsetHeight;
}
this.obj = id+'Object';
eval (this.obj +' =this');
return this;
}

/********************************************************************************************
index****************************************************************************************/

function index1(theObj,px,py,pjx,pjy,pex,pey,wait,sc)
// ok, some explaining here:
//you would acces this function by using unload in the body tag. :
//<body onload=index1('idofdiv','outleft',0,5,5,'center',100,1)
//theObj is the id of the div (a string at this point). px and py are starting points of the div.
//pjx and pjy are the values the object jumps at each interval.
//pex, and pey are the coordinates where the div stops. x is left (0) to right and y is top (0) to bottom
//the beauty of this program is that you can insert string like center and right and the programm finds the
// coordinates on its own;
//wait is the for the timeout settings. how long it waites before removing it. in millisecs and the smaller the
//faster
//sc is another sweet thing. it stands for shortCut. it calculates the straight line between 2 points! 1 means you
// want it 0 means... hard to explain.. check it out....
{
px=getPos(theObj,px); //in the case you used presettings like 'center' it will check it out now.
py=getPos(theObj,py); //dito
pex=getPos(theObj,pex); //dito
pey=getPos(theObj,pey); //dito
var fs=true; //stands for firstSlide... i'll tell you later...
if(sc==1) //check out if you want a shortcut and calculates it
{
var tempx=(pex-psx)/pjx;
var tempy=(pey-psy)/pjy;
//the ending point minus the start point are the distance on a axis. divided by the jumpdistance gives
//you the amount of jumps.

if(tempx==tempy){pjx=pjx; pjy=pjy;}
else if (tempx>tempy){pjx=pjx; pjy=(pey-psy)/tempx;}
else if (tempx<tempy){pjx=(pex-psx)/tempy; pjy=pjy;}
else {alert('not defined');}
//here we check for the smaller amount of jumps. if tempx is smaller it has to jump a bigger distance on the y
// axis how do we get that? by dividing the distance of the y axis by the smaller amount of jumps.
}

//alert(pjx + " " + pjy) --used to check if it works

slide(theObj,px,py,pjx,pjy,pex,pey,wait,fs); //let's move it baby!
}

function slide(theObj,px,py,pjx,pjy,pex,pey,wait,fs)
//those are a lot of references, i know, but i'ld hate to make them global if i don;t use it in my api and
//i really don;t like to change everything by hand. this way I'll change the references later in the body-tag
//and don't even bother about my api.js
{
if (fs) //ok, now i tell you. is it in the slide function the first time? (fs) means (fs==true) if yes:
{
fs=false; //it won't be the first time the next time, change fs
shiftTo(theObj,px,py); //send it to shiftTo which positiones the div on the page.)
setTimeout('slide("'+theObj+'" ,'+px+','+py+','+pjx+','+pjy+','+pex+','+pey+','+wait+','+fs+')',wait);
//there is no timeout needed, but i just added it here. it calls the slide function again and sends the
//references. that's the difficult part:
//if the ref is a string send it this way: " '+stringinthiscasidofdiv+' " ( , if there are more)
//if it is a number: '+num+' ( , if there are more)
//it is critical to NOT leave a space after the ' when sending a string. it would add an empty space
//*took me hours to find that error!
}
else //if it is not the first Time anymore
{
if ((px<=pex)&&(py<=pey)) //if the coordinates are smaller than the endcoordinates...
{
px +=pjx; //add the jumpdistance to get the new coordinate ...
py +=pjy; //...for the next jump...
shiftTo(theObj,px,py); // ...change the position now...
setTimeout('slide("'+theObj+'",'+px+','+py+','+pjx+','+pjy+','+pex+','+pey+','+wait+','+fs+')',wait);
//and wait a little bit before you do it again... easy!
}
else if ((px>pex)&&(py<=pey)) //what if one coordinate already passed the limit-value?
{
py +=pjy; //just add to the value which still is smaller
shiftTo(theObj,px,py); //send it
setTimeout('slide("'+theObj+'",'+px+','+py+','+pjx+','+pjy+','+pex+','+pey+','+wait+','+fs+')',wait);
//and wait
}
else if ((px<=pex)&&(py>pey)) //mostly the same as above
{
px +=pjx; shiftTo(theObj,px,py);
setTimeout('slide("'+theObj+'",'+px+','+py+','+pjx+','+pjy+','+pex+','+pey+','+wait+','+fs+')',wait);
}
else {alert('DANG IT!';} //well, now you know it didn;t work!
}
}

/*********************************************************************************************
Utilitiy functions*****************************************************************************/

function shiftTo(theObj,x,y) //What about a shiftBy?--note for myself...
//Positioning an object at a specific pixel coordinate
{
var obj=new makeObj(theObj); //make the div id an object
if (typeof x != "number") {x=getPos(theObj,x)} //checks if it is a string (that's the beauty...) or a number
if (typeof y != "number") {y=getPos(theObj,y)} //and gets the right values if it is
obj.css.left = x;
obj.css.top = y; //assigns the new coordinates to the obj.
}

function getPos(theObj,pos)
//this one converts the strings into numbers whith which the pc can work... browser i mean.
{
var obj=new makeObj(theObj); //make div id an object
var page = new pageSize; //get the page sizes
if (typeof pos == "number") {pos=pos;} // if it is a number dont'change it
//positions on the x axis.
//you can have those kinds of strings:
else if (pos=="outleft"){pos=0-obj.w;} //outleft is left of the window by a object width
else if (pos=="left"){pos=0;}//left
else if (pos=="center"){pos=(page.w-obj.w)/2;}//windowwidth-objectwidth divided by 2 to center it
else if (pos=="right"){pos=page.w-obj.w;}//windowwidth-objectwidth
else if (pos=="outright"){pos=page.w+obj.w;}//on the right side of the window.
//positions along the y axis.
else if (pos=="outtop"){pos=0-obj.h;}//you get the idea...
else if (pos=="top"){pos=0;}
else if (pos=="middle"){pos=(page.h-obj.h)/2;}
else if (pos=="bottom"){pos=page.h-obj.h;}
else if (pos=="outbottom"){pos=page.h+obj.h;}
else {alert('not defined');}
return pos;
}

function pageSize()
//window: inner Width&Height and middle,center
{
this.w = (br.n) ? innerWidth:document.body.offsetWidth;
this.h = (br.n) ? innerHeight:document.body.offsetHeight;
this.w2 = this.x/2;
this.h2 = this.y/2;
return this
}

[hr]

thanks to bitdamaged because the makeObject-Idea came from him. and shadowimage he showd me the refernece thing.
and thanks to the others.
I will add better versions like not having to add - signs in front of the jumpvalue in the body tag
as soon you want it to slide up or left...

k10

« BackwardsOnwards »

Show Forum Drop Down Menu