TransWikia.com

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

Stack Overflow Asked by Agiftel Longwave on November 22, 2021

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;
      },
    });
  }
});

3 Answers

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.

Answered by Emre Koc on November 22, 2021

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);
});

Answered by Fatihd on November 22, 2021

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);
  }
});

Answered by AKX on November 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP