how to use setTimeout function

  • Thread starter Thread starter qasim
  • Start date Start date
Q

qasim

I have a function:

function func(a,b)
{
alert(a+b);
}


This syntax for setTimeout() function is not working:

setTimeout("func(a,b)",1000)


Please help me

Qasim zeeshan
 
setTimeout takes either a string which it will do an eval of, or pass it
it a delegate. closure will take effect for the parameters passed to a
delegate:

function caller1(a)
{
// references global a
window.setTimeout("alert(a);",1000);
}
function caller2(a)
{
window.setTimeout(function()
{
// references passed a
alert(a)
},1000);
}

caller1(1); // alert will throw error
caller2(1); // alert shows 1
var a=3;
caller1(2); // alert shows 3
caller2(2); // alert shows 2

-- bruce (sqlwork.com)
 
Look good to me except you do not pass real values to the func. func(a,b)
and a and b is not set to anything..

try
setTimeout("func(2,4)",1000)
it will work.

If you need to pass values you can do

var a,b;
a = 1;
b = 4;
setTimeoout("fun(" + a + "," + b +")", 1000);

It will work.

George.
 
Back
Top