user2598957
user2598957

Reputation: 263

How can I delay javascript from running

I have a script that I would like to delay from running for 2 seconds maybe 3 then execute, at the moment I use document.ready is there a way to add a time value so its still document.ready but add 3 seconds.

$(document).ready(function() {
   function bikepass2(){
     bike.css('left', startPos);
     bike.animate({left: -450}, 8000, 'linear')
   };

    var screenWidth = $(document).width();
    var startPos = screenWidth;
    var bike = $('#bike2')
    bikepass2();
    setInterval(function() {
      bikepass2();
    }, 15000);
  });

Upvotes: 0

Views: 71

Answers (3)

edonbajrami
edonbajrami

Reputation: 2206

Add the function inside a setTimeout. http://www.w3schools.com/jsref/met_win_settimeout.asp. In the time add 3000 for 3 seconds.

setTimeout( function() 
{
    //add the function that you want to execute after 3 seconds

}, 3000);

Upvotes: 2

Daniel Müller
Daniel Müller

Reputation: 411

Javascript has no sleep() function, which causes the script to pause.

But you can use .setTimeout() to run a method delayed:

window.setTimeout(myFunc(), 1000);

Upvotes: 1

MysticMagicϡ
MysticMagicϡ

Reputation: 28823

Try this:

setTimeout( function() 
{
    //do something special
}, 3000);

Put in inside document.ready()

Hope it helps.

Upvotes: 2

Related Questions