PDA

View Full Version : calling a function from SetTimeout


ShMiL
05-08-2003, 09:05 AM
I tried all the possible versions but still couldn't figure this out!

How can I do this:

function fA(x) {setTimeout("x.style.color='red';",1000);}

(using the parameter input to fA:x, inside the setTimeout)

I tried something like this:
function fA(x) {var str=x + ".style.color='red';";setTimeout(str,1000);}

I even tried what glenn wrote here:
http://www.codingforums.com/showthread.php?s=&threadid=12474&highlight=setTimeout+parameters

But it didn't work as well...
Anyone ever tried it?
Thanks

liorean
05-08-2003, 10:17 AM
The setTimeout executes something in the global scope, while x only exists in the scope of fA. Try using Algorithm's Timer class: <http://codingforums.com/showthread.php?s=&threadid=10531>

ShMiL
05-08-2003, 10:36 AM
I'm really sorry for being a pain in the *** but my JS is not that good and I don't know where to put what and how...
Can you refer me to a page that works with it so I can copy it like a monkey, without even understanding it...?

Algorithm
05-08-2003, 10:41 AM
Assuming the Timer class is included:function fA(x) { fA.timer.setTimeout('run',1000,x); }
fA.timer = new Timer(fA);
fA.run = function(x){ x.style.color='red'; }

ShMiL
05-08-2003, 10:50 AM
Thanks alot!
But what is "class", how can I "include" it?

liorean
05-08-2003, 04:11 PM
By including the Timer class, he means to declare the Timer object in your source code.

ShMiL
05-08-2003, 04:15 PM
How is it done? How can one declare the Timer object in his source code?

liorean
05-08-2003, 04:28 PM
Just copy the source code from Algorithm's post to your script.

ShMiL
05-08-2003, 06:29 PM
I put this code in theTimer.js file:

// The constructor should be called with
// the parent object (optional, defaults to window).

function Timer(){
this.obj = (arguments.length)?arguments[0]:window;
return this;
}

// The set functions should be called with:
// - The name of the object method (as a string) (required)
// - The millisecond delay (required)
// - Any number of extra arguments, which will all be
// passed to the method when it is evaluated.

Timer.prototype.setInterval = function(func, msec){
var i = Timer.getNew();
var t = Timer.buildCall(this.obj, i, arguments);
Timer.set[i].timer = window.setInterval(t,msec);
return i;
}
Timer.prototype.setTimeout = function(func, msec){
var i = Timer.getNew();
Timer.buildCall(this.obj, i, arguments);
Timer.set[i].timer = window.setTimeout("Timer.callOnce("+i+");",msec);
return i;
}

// The clear functions should be called with
// the return value from the equivalent set function.

Timer.prototype.clearInterval = function(i){
if(!Timer.set[i]) return;
window.clearInterval(Timer.set[i].timer);
Timer.set[i] = null;
}
Timer.prototype.clearTimeout = function(i){
if(!Timer.set[i]) return;
window.clearTimeout(Timer.set[i].timer);
Timer.set[i] = null;
}

// Private data

Timer.set = new Array();
Timer.buildCall = function(obj, i, args){
var j, t="";
Timer.set[i] = Timer.getData(obj);
if(obj != window){
t = "Timer.set["+i+"].obj.";
}
t = t + args[0] + "(";
for(j=0; (j+2)<args.length; j++){
Timer.set[i][j] = args[j+2];
if(j>0) t = t + ", ";
t = t + "Timer.set["+i+"]["+j+"]";
}
t = t + ");";
Timer.set[i].call = t;
return t;
}
Timer.callOnce = function(i){
if(!Timer.set[i]) return;
eval(Timer.set[i].call);
Timer.set[i] = null;
}
Timer.getData = function(obj){
var data = new Array();
data.obj = obj;
data.call = "void(0);";
data.timer = null;
return data;
}
Timer.getNew = function(){
var i = 0;
while(Timer.set[i]) i++;
return i;
}

And I call it from the <head> like this: <script type="text/javascript" src="theTimer.js"></script>

And put this:
function SearchKeyDown(whatCell) {SearchKeyDown.timer.setTimeout('run',1000,whatCell);}
SearchKeyDown.timer = new Timer(SearchKeyDown);
SearchKeyDown.run = function(whatCell){setTimeout("whatCell.style.color='red';"}
in the script too...

I call the function from an input like this:
<input onKeyDown="javascript:SearchKeyDown(this);" ...>

And I still get an error...
Where did I do it wrong?

liorean
05-08-2003, 06:34 PM
What error did you get? What browser did you use? What line did it occur on?

Just telling us you got an error doesn't give us much to work with.

ShMiL
05-08-2003, 06:59 PM
your'e right, sorry.
The error is: "object expected"
it occurs after I write something in the input...
The error points to the line where it says:
<input onKeyDown="javascript:SearchKeyDown(this);" ...>

Algorithm
05-08-2003, 10:30 PM
You don't need the javascript: definition when assigning events.

Try this: <input onKeyDown="SearchKeyDown(this);" ...>

Edit: I also just noticed an error in my previous post, which has been corrected. You need to remove the setTimeout call in the .run() method.

ShMiL
05-09-2003, 05:13 AM
great!
it's working now!

Thanks~~~~! :thumbsup: :thumbsup:

glenngv
05-09-2003, 05:48 AM
function fA(x) {setTimeout("document.getElementById('"+x.id+"').style.color='red';",1000);}

ShMiL
05-09-2003, 10:34 AM
The simpler the better :)
Thanks glenn!