tags:

views:

67

answers:

5

I need to execute a function in javascript named demo() and sample() optionally.ie,first my program will wait for 5 seconds to execute demo(); if it is fail to start demo with in 5 seconds i need to execute sample() automatically (from javascript).is it possible to do in javascript?Please help me....Thanks

A: 

You can use setTimeout for a pause in javascript.

setTimeout(function(){callSample();}, 5000);

then set a global variable inside demo() so that you can identify whether demo() has been called or not and then in

function callSample()
{
    if (variable set)
    {
        sample();
    }
}
rahul
+1  A: 

Have a look at:

Example:

setTimeout(function(){your code here}, 3000)
Sarfraz
+2  A: 

You can invoke functions after a period of time with setTimeout

setTimeout(demo, 5000);

I'm not sure that I get the "if it is fail to start demo with in 5 seconds" part of your question, because the above code will execute demo() in 5 seconds.

GlenCrawford
A: 

<script language="javascript"> function sample() { alert('sample here'); } function demo() { alert('demo here'); } setTimeout("sample()", 5000); </script> <input type=button onclick="demo();">

Jobst
very good explanation!thank you so much.But, actually your script will involk both function with the interval of 5 seconds.actually i need only one function at a time.
Ajith
A: 

Here's a demo of setTimeout

unigg