Ronal Vasquez
Ronal Vasquez

Reputation: 119

wait or sleep in javascript

as I can pause this code of 1 second before running the "post function".

$(document).ready(function() { 
    $("#Button").click(function() {

        /*1 second pause before executing the post */
        $.post(url,{Par1=5},function(e){
        });
    });
});

Regards.

Upvotes: 2

Views: 297

Answers (3)

Andre Hofmeister
Andre Hofmeister

Reputation: 3416

You can use the setTimeout(code,millisec,lang) function. Here some informations.

As result on the comments you will find here a better summary, without the lang parameter.

setTimeout(function() {
  $.post(url,{Par1=5},function(e){
  });
}, 1000);

Upvotes: 1

Paul
Paul

Reputation: 141827

Yes, use setTimeout with an argument of 1000 milliseconds:

$(document).ready(function() { 
    $("#Button").click(function() {

        setTimeout(function(){
            $.post(url,{Par1=5},function(e){
                // ...
            });
        }, 1000);
    });
});

Upvotes: 1

Rory McCrossan
Rory McCrossan

Reputation: 337570

You can use setTimeout:

$("#Button").click(function() {
    setTimeout(function() {
        /* 1 second pause before executing the post */
        $.post(url, { Par1 = 5 }, function(e) { } );
    }, 1000);
});

Also, assuming you only want multiple clicks of the button to send 1 request at a time, you can use clearTimeout to prevent your site being flooded. Try this

var timeout;
$("#Button").click(function() {
    clearTimeout(timeout);
    timeout = setTimeout(function() {
        $.post(url, { Par1 = 5 }, function(e) { } );
    }, 1000);
});

Upvotes: 2

Related Questions