how to use setTimeout function

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
 
B

bruce barker

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)
 
G

George

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top