programing

jQuery Refresh/Ajax Success가 예상 시간 이후인 경우 페이지 다시 로드

easyjava 2023. 9. 26. 22:38
반응형

jQuery Refresh/Ajax Success가 예상 시간 이후인 경우 페이지 다시 로드

저는 Jquery를 사용하여 Ajax를 요청합니다.서버는 다음과 같이 "true or false" 값을 가진 Json 개체를 반환합니다.

return Json(new { success = false, JsonRequestBehavior.AllowGet });

서버가 true를 반환하면 5초 후에 페이지를 새로 고칠 방법이 있습니까?

Ajax 성공 콜백 시 다음 작업을 수행합니다.

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); 
   }
}

5초 후에 페이지를 다시 로드하려면 답변에 제시된 대로 타임아웃이 필요합니다.

location.reload();

사용할 수 있습니다.reload당신의 기능을 합니다.if조건이 성공하면 페이지가 다시 로드됩니다.

if(success == true)
{
  //For wait 5 seconds
  setTimeout(function() 
  {
    location.reload();  //Refresh page
  }, 5000);
}

이 방법이 더 좋습니다.ajaxStop+setInterval,, 이것은 같은 페이지에서 XHR[ajax] 요청 후에 페이지를 새로 고칩니다.

    $(document).ajaxStop(function() {
        setInterval(function() {
            location.reload();
        }, 3000);
    });
var val = $.parseJSON(data);
if(val.success == true)
{
 setTimeout(function(){ location.reload(); }, 5000);

}
$.ajax("youurl", function(data){
    if (data.success == true)
    setTimeout(function(){window.location = window.location}, 5000); 
    })
)

오늘 이것을 조사한 후 호기심으로 여기에 많은 좋은 답이 있습니다. 사용하는 것이 최선이 아닙니다.setInterval보다setTimeout?

setInterval(function() {
location.reload();
}, 30000);

당신의 생각을 알려주세요.

그냥 사용.

    $.ajax({
        success: function (response) {
            location.reload();
            },
        error : function(xhr,errmsg,err) {
        console.log(xhr.status + ": " + xhr.responseText); // provide a bit more info about the error to the console
    }
    });

언급URL : https://stackoverflow.com/questions/27654298/jquery-refresh-reload-page-if-ajax-success-after-predicted-time

반응형