Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Function After Delay

Tags:

jquery

I have the below global jQuery function stored, but on page load, I want to execute it after a 1000 delay. Is there something wrong with my syntax? I know the delay always goes before the function. It is not responding.

Global function:

function showpanel() {             $(".navigation").hide();        $(".page").children(".panel").fadeIn(1000);     ;} 

Executing function:

parallax.about.onload=function(){     $('#about').delay(3000).showpanel(); }; 
like image 624
Joe Isaacson Avatar asked Jun 02 '12 05:06

Joe Isaacson


People also ask

How do you execute a function after 5 seconds?

A setTimeout is a native JavaScript function, which calls a function or executes a code snippet after a specified delay (in milliseconds). A setTimeout accepts a reference to a function as the first argument. Alert messages will pop up after 5 seconds automatically.


2 Answers

$(document).ready(function() {    // place this within dom ready function   function showpanel() {          $(".navigation").hide();     $(".page").children(".panel").fadeIn(1000);  }   // use setTimeout() to execute  setTimeout(showpanel, 1000)  }); 

For more see here

like image 67
thecodeparadox Avatar answered Sep 21 '22 06:09

thecodeparadox


I searched and found the solution in the following URL is better.

http://www.tutorialrepublic.com/faq/call-a-function-after-some-time-in-jquery.php

It worth to try.

It adds your given function to the queue of functions to be executed on the matched element which is currently this.

 $(this).delay(1000).queue(function() {       // your Code | Function here            $(this).dequeue();      }); 

and then execute the next function on the queue for the matched element(s) which is currently this again.

-- EDIT [ POSSIBLE EXPLANATION FOR THE DEQUEUE COMMAND ]

Take a look at the command

We command the jQuery engine to add a function in internal queue and then after a specific amount of time we command it to call that function, BUT so far we never told it to dequeue it from engine. Right?! And then after every thing is done we are dequeue it from jQuery engine manually. I hope the explanation could help.

like image 21
ebrahim.mr Avatar answered Sep 20 '22 06:09

ebrahim.mr