0

I need to add 2 sec sleep before window.location.href runs.

For example:

$.ajax({
    url:"price",
    method:"POST",
    }).done(function (data) {
        var origin = window.location.origin;
        // I need add 2 sec sleep
        window.location.href = origin + "/flight/ticket/" + data;
    }).error(function (xhr, opt, error) {
        $('#trustCaptcha').show();
        $('#getTicket').hide();
    }
);

Thank you.

JonyD
  • 1,127
  • 1
  • 20
  • 32
mySun
  • 1,610
  • 4
  • 28
  • 49

2 Answers2

2

You can use setTimeout() method.

$.ajax({
  url: "price",
  method: "POST",
}).done(function(data) {
  var origin = window.location.origin;
  // I need add 2 sec sleep
  setTimeout(function() {
    window.location.href = origin + "/flight/ticket/" + data;
  }, 2000);
}).error(function(xhr, opt, error) {
  $('#trustCaptcha').show();
  $('#getTicket').hide();
});

You can read more about this method here.

Ionut
  • 10,707
  • 4
  • 40
  • 69
1

You can use setTimeout() to delay logic execution. Try this:

$.ajax({
  url:"price",
  method:"POST",
}).done(function (data) {
  var origin = window.location.origin;
  setTimeout(function() {
    window.location.href = origin + "/flight/ticket/" + data;
  }, 2000); // 2000ms = 2 seconds
}).error(function (xhr, opt, error) {
  $('#trustCaptcha').show();
  $('#getTicket').hide();
});
Rory McCrossan
  • 319,460
  • 37
  • 290
  • 318