There was such a problem, the function makes a check every second, but if an exception occurs in the PHP-коде , I need to suspend setInterval for 60 seconds and then restore its check every second. How can this be implemented?

 function show() { $.ajax({ url: "test.php", cache: false, success: function(data) { $("#content").html(data); } }); } $(document).ready(function() { setInterval('show()', 1000); }); 

  • If instead of a specific time delay (1000ms) substitute a variable that will dynamically change depending on which data came from the server and came, did you try? - haswell

1 answer 1

For example, return from the server JSON, in which, if the exception isException = true . Customer example:

 var interval; function show() { $.ajax({ url: "test.php", cache: false, success: function(data) { let object = JSON.parse(data); if (object.isException) { restartInterval() } $("#content").html(data.html); }, error: function() { // если выброшена 500, например, ошибка, будет вызван этот метод. restartInterval() } }); } function restartInterval() { clearInterval(interval); setTimeout(() => interval = setInterval(show, 1000), 60000); } $(document).ready(function() { interval = setInterval(show, 1000); });