Home » Jquery » javascript – Best practices of timeout after Ajax call

javascript – Best practices of timeout after Ajax call

Posted by: admin February 22, 2020 Leave a comment

Questions:

I have implemented a timeout after my table is reloaded with new data, previously. I was having trouble on how to update my dom after ajax was called as I’m developing front end js on top of an analytical webapp. I’ve come to a solution to use timeout which will load the dom after its refreshed but I am unsure if this is the best practice as I have to preset the time and different view will have different queries and rendering time.

  $( document ).ajaxSuccess(function( event, xhr, settings ) {
     setInterval(function(){ 
        let update = d3.selectAll("#" + arcapi.chartId() + ' tbody td')
        update.filter(function(){
           if(this.innerHTML === 'Tier 2'){
             return true;
            }
        }).style('background-color','green');
     }, 2000); 
  });

Note: I could not directly update from ajax code right after ajaxComplete is because I don’t have access ajax file. This is the best solution I have so far in 8 days. Do let me know your input and opinion.

How to&Answer:

You could follow below format as it mostly recommended among the engineers.
You could also read this official document

$.ajax({
        url: "YourRequestURL",
        error: function(){
            // This function would triggered when timeout occurred 
        },
        success: function(){
            //Your logic after success request 
        },
        timeout: 3000 // sets timeout 1 to 3 seconds is mostly used
    });

Though there is no concrete rules for timeout but its recommended to
set it between 1000-5000. However, as per your requirement it might change.

Hope this would help