1

Can someone assist me on how I can delay this function?

$('#customer_quote').lightbox_me({  
        centered: true, 
        closeSelect: ".close",
        onClose: function(){
            $('div.error').remove()
            }
        })  

 });

I want this lightbox to open after 7 seconds.

breezy
  • 1,910
  • 1
  • 18
  • 35

5 Answers5

8

Use setTimeout():

setTimeout(lightbox, 7000);

function lightbox() {
    $('#customer_quote').lightbox_me({  
            centered: true, 
            closeSelect: ".close",
            onClose: function(){
                $('div.error').remove()
                }
            })  

     });
}
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • can you break down the code for me? so i know what happened? @Rory – breezy Aug 10 '12 at 15:36
  • 1
    The code is exactly the same as you had originally, the two differences are that I wrapped it in a function - `lightbox()` you can change this to be something more relevent if you need. The important part is the `setTimeout()`. You provide this method with a function to run, and the delay (in ms) defining when the function should be called. – Rory McCrossan Aug 10 '12 at 15:38
4

Give setTimeout a shot:

setTimeout(function() {
    $('#customer_quote').lightbox_me({
        centered: true,
        closeSelect: ".close",
        onClose: function() {
            $('div.error').remove()
        }
    });
}, 7000);​
Brian Ustas
  • 62,713
  • 3
  • 28
  • 21
3
setTimeout(function(){
$('#customer_quote').lightbox_me({  
        centered: true, 
        closeSelect: ".close",
        onClose: function(){
            $('div.error').remove()
            }
        })  

 });

}, 7000);
ek_ny
  • 10,153
  • 6
  • 47
  • 60
2

Check this link out: To delay JavaScript function call using jQuery

Seems like you just use setTimeout with 7000

Community
  • 1
  • 1
MikeB
  • 2,402
  • 1
  • 15
  • 24
1

Use setTimeout.

var delayedFunction = function() {
   /* your code */
}

setTimeout(delayedFunction, 7000);

The second argument stands for the number of miliseconds.

Note also that this evokes an asynchronous event. The execution of your code will not stop at the line with setTimeout for 7 seconds.

If you want to execute another code after this delay, you must do so in delayedFunction, when the event fires.

Imp
  • 8,409
  • 1
  • 25
  • 36
  • This will keep calling `delayedFunction()` until `clearInterval()` is called. I do not think this is what the OP wants. – A Person Aug 10 '12 at 15:32
  • @siidhesh Sorry, I edited `setInterval` to `setTimeout` about a second after I posted the answer. I used it in my recent code more often, so consider that a typo :) – Imp Aug 10 '12 at 15:34