1

How to call a jquery function onload with some delay?

I want to call a function like this on load with some delay

$(".sample").live('click',function(){
        var id=$(this).attr("id");
4
  • So you want to assign the event handler after a delay? Commented Oct 17, 2012 at 9:08
  • @Archer I think he wants to execute handler after a delay. Commented Oct 17, 2012 at 9:08
  • nope i just want to call a function with some delay onload , Commented Oct 17, 2012 at 9:09
  • possible duplicate of Jquery delay on function Commented Jul 19, 2014 at 2:01

5 Answers 5

6

This will wait 1 second and then assign the event handler...

function assignSampleClick() {
    $(".sample").live('click',function(){
        var id=$(this).attr("id");
    });
}

// document ready
$(function() {
    setTimeout(assignSampleClick, 1000);
});
Sign up to request clarification or add additional context in comments.

Comments

3

Try it like this..

$(document).ready(function () {
setTimeout(function () {
$(".sample").live('click',function(){
var id=$(this).attr("id");
   }, 3000);
}, 3000);

This will fire on page load after 3 seconds delay.

Comments

0

you could use setTimeout function you must refactor your code as something like this

    //2 seconds wait before calling doJob
    $(".sample").live('click',function(){ setTimeout(doJob, 2000);});

    function doJob(){
        var id=$(this).attr("id");
        //other statements ...
    }

1 Comment

He wants it to wait before assigning the event handler, not before running the attached code ;)
-1

Try to use Timer Plugin

$(this).oneTime(1000, function() {
     // some action here
});

Comments

-1

try this

   var demora = window.setInterval(lerolero, 3000);
function lerolero() {
     $("#ovo").css("display", "block");
	}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.