-3

How to run javascript/Jquery functions on user specific time scenario its so simple when i click HTML button the result will be show after 20 sec

<script>
function setTime(){
   alert("Ok");
}
</script>

<input type ="button" onclick="setTime()" value="Click">

Jquery SetInterval,Delay function i don't know how do i implement this please help me out or helps are definitely appreciated

Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
Query Master
  • 6,989
  • 5
  • 35
  • 58
  • 2
    Have you done *any* research? A simple Google search gave me http://ajaxian.com/archives/delaying-javascript-execution There are also [some](http://stackoverflow.com/questions/5372106/delay-javascripts-function-execution) [related](http://stackoverflow.com/questions/3879768/best-way-to-put-delay-after-calling-javascript-functions) [SO questions](http://stackoverflow.com/questions/3301398/how-to-add-delay-before-calling-next-call-back-function-in-jquery). – Felix Kling Apr 02 '12 at 14:59
  • when i was search this logic in google i found Jquery delay function but i didn't understand how do i implement this logic with delay – Query Master Apr 02 '12 at 15:04
  • @Samad: jQuery's `delay` is only for jQuery animations. – gen_Eric Apr 02 '12 at 15:05
  • That why i put this Quertion here – Query Master Apr 02 '12 at 15:05
  • 1
    @Rocket: You can use it with other functions/queues too, but `setTimeout` is just simpler. That's also explicitly mentioned in the [documentation](http://api.jquery.com/delay/). – Felix Kling Apr 02 '12 at 15:07
  • what issue is this Question i don't know @Felix Kling – Query Master Apr 02 '12 at 15:07
  • Thanx Guys for sharing your experience with me – Query Master Apr 02 '12 at 15:25

4 Answers4

3

What you are looking for is setTimeout:

Executes a code snippet or a function after specified delay.

For example:

function setTime(){
  window.setTimeout(
      function() {
          // Your code here...
      },
      20 * 1000);
}
Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
2

Use setTimeout if you want to execute anything after certain delay and use setInterval if you want to execute some piece of code after every interval with some delay.

In your case setTimeout fits the best. Try this.

function setTime(){
   setTimeout(function(){
      alert("Ok");
   }, 20 * 1000);//provide the delay in milliseconds
}
ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
2
<script>
$(document).ready(function(){
   setTimeout(setTime, 20000);
   function setTime(){ 
      alert("Ok"); 
   }   
});
</script>
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
aKzenT
  • 7,775
  • 2
  • 36
  • 65
1
setTimeout(func, delay);

The 1st parameter is the function you want to run, the 2nd is the delay (in milliseconds).

DOCS: https://developer.mozilla.org/en/DOM/window.setTimeout

gen_Eric
  • 223,194
  • 41
  • 299
  • 337