Execution of setTimeout with replaceWith

Asked

Viewed 33 times

0

I have the following code that executes a query in DB with ajax. So when in Success I put append instead of replaceWith, the function works perfectly and runs every 5 seconds, but when I enter replaceWith in place of the append, the function only runs once and does not execute setimeout.

PS: php variables appear like this because the function is inside a php query.

function fetchdata". $row2['id'] ."(){
    var dz='". $row2['id'] ."';     
    var dld='$jmmj';

 $.ajax({
 type: 'POST',
    data: {dz: dz},
  url: 'submit4.php',


  success: function(datas){
    if(datas!==''){
   $('#message18w". $row2['id'] ."').replaceWith(datas);


     }else{ $('#message18w". $row2['id'] ."').replaceWith('');}
     setTimeout(fetchdata". $row2['id'] .",5000);
  },
    error:function(datas){
   setTimeout(fetchdata". $row2['id'] .",5000);
  },
  complete:function(datas){
   setTimeout(fetchdata". $row2['id'] .",5000);
  }
 });
}

$(document).ready(function(){
 setTimeout(fetchdata". $row2['id'] .",5000);
});

1 answer

0

There’s a problem with your code:

You put a setTimeout within the "Success", "error" and "complete" blocks, so when Ajax returns ok, it will call the 2 setTimeouts and will cause a bottleneck in your server and even lock the browser.

You must leave the setTimeout only within the "complete", because this block will always be called after the return of Ajax, giving error or not.

  • even so the problem persists, this setTimeout was introduced by me in the search for a solution

  • I already solved the problem, I was not even in that code piece but in what I put as replace

  • That’s what I was about to say. Good q solved! Good luck!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.