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,237
  • 3
  • 21
  • 34
mySun
  • 1,550
  • 5
  • 32
  • 52

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 Necula
  • 11,107
  • 4
  • 45
  • 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
  • 331,213
  • 40
  • 305
  • 339