Execution of setTimeout with replaceWith

0

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

PS: php variables appear so 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);
});
    
asked by anonymous 29.08.2017 / 17:13

1 answer

0

You have a problem with your code:

You put a setTimeout inside the blocks "success", "error" and "complete", so when Ajax returns ok, it will call the 2 setTimeouts and it will cause a bottleneck on your server and even stop the browser.

You should leave setTimeout just inside the "complete", because this block will always be called after the return of Ajax, giving error or not.

    
29.08.2017 / 18:00