jQuery Refresh/Reload Page if Ajax Success after time

前端 未结 7 581
心在旅途
心在旅途 2020-12-14 02:42

I use Jquery to make an Ajax request. The server returns Json object with value "true or false" like this:

return Json(new { success = false, JsonRe         


        
相关标签:
7条回答
  • 2020-12-14 03:20
    location.reload();
    

    You can use the reload function in your if condition for success and the page will reload after the condition is successful.

    0 讨论(0)
  • 2020-12-14 03:22
    var val = $.parseJSON(data);
    if(val.success == true)
    {
     setTimeout(function(){ location.reload(); }, 5000);
    
    }
    
    0 讨论(0)
  • 2020-12-14 03:24
    if(success == true)
    {
      //For wait 5 seconds
      setTimeout(function() 
      {
        location.reload();  //Refresh page
      }, 5000);
    }
    
    0 讨论(0)
  • 2020-12-14 03:27

    In your ajax success callback do this:

    success: function(data){
       if(data.success == true){ // if true (1)
          setTimeout(function(){// wait for 5 secs(2)
               location.reload(); // then reload the page.(3)
          }, 5000); 
       }
    }
    

    As you want to reload the page after 5 seconds, then you need to have a timeout as suggested in the answer.

    0 讨论(0)
  • 2020-12-14 03:35

    I prefer this way Using ajaxStop + setInterval,, this will refresh the page after any XHR[ajax] request in the same page

        $(document).ajaxStop(function() {
            setInterval(function() {
                location.reload();
            }, 3000);
        });
    
    0 讨论(0)
  • 2020-12-14 03:42
    $.ajax("youurl", function(data){
        if (data.success == true)
        setTimeout(function(){window.location = window.location}, 5000); 
        })
    )
    
    0 讨论(0)
提交回复
热议问题