Agiftel Longwave
Agiftel Longwave

Reputation: 47

How to slow down window.location.href on AJAX request

I need to find a way to slow down the reload of page that window.location.href does. This is needed because the animation of toggle button is not shown. Is there a way?

Here code used:

$(".closeMan").change(function () {
  if ($(this).prop("checked")) {
    $.ajax({
      type: "POST",
      url: "./close_man.php",
      success: function (result) {
        window.location.href = window.location.href;
      },
    });
  }
});

Upvotes: 0

Views: 869

Answers (3)

Emre Koc
Emre Koc

Reputation: 1588

You can use a timeout, and window.location.reload():

setTimeout(function() {
    window.location.reload();
}, 1000); // Time in milliseconds

Note: you cant call it like setTimeout(window.location.reload, 1000) because you will get an Illegal invocation error.

Upvotes: 1

Fatihd
Fatihd

Reputation: 645

You may use settimeout to handle timing problem.

$(".closeMan").change(function () {
  if ($(this).prop("checked")) {
    setTimeout(function(){   
       $.ajax({
        type: "POST",
        url: "./close_man.php",
        success: function (result) {
         window.location.href = window.location.href;
        },
       });
      }
     }, 1000);
});

Upvotes: 0

AKX
AKX

Reputation: 169407

Simply use setTimeout (if you really need to slow down your application...):

$(".closeMan").change(function () {
  if ($(this).prop("checked")) {
    setTimeout(() => {
      $.ajax({
        type: "POST",
        url: "./close_man.php",
        success: () => {
          window.location.href = window.location.href;
        },
      });
    }, 500);
  }
});

Upvotes: 2

Related Questions