programing

jQuery.get()으로 시간 초과

telecom 2023. 10. 7. 09:24
반응형

jQuery.get()으로 시간 초과

시간 초과 기간을 설정하는 방법이 있습니까?$.get()?

스크립트가 로딩에 시간이 좀 걸린다는 경고를 주기를 원하기 때문에(아마도 사용 중인 서버 등으로 인해) 어디에서도 찾을 수 없는 것 같습니다.

사용하기만 하면 됩니다.

$.ajax({
   url: url,
   success: function(data){
        //...
   },
   timeout: 1000 //in milliseconds
});

또한 아래 설명과 같이 타임아웃을 전역적으로 적용하는 데 사용할 수 있습니다.

$.ajaxSetup({timeout:1000}); //in milliseconds

$.get 그것은 단지 좋은 축약어입니다.timeout옵션: 스크립트가 콜백을 취소할 때까지 기다리는 시간을 지정합니다.

을(를) 사용하여 글로벌 기본값을 재정의할 수 있으며, 이를 통해 계속 사용할 수 있습니다.$.get.

요청을 취소하지 않고 단순히 사용자에게 알림을 주고 싶다면 다음을 사용합니다.$.ajax, 시간 초과를 설정하고 시간 초과를 취소합니다.complete콜백

스타터 코드의 예시는 다음과 같습니다. 테스트를 한 적이 없습니다. 약간의 작업을 통해 플러그인으로 변환될 수도 있습니다.

(function(){
  var t, delay;
  delay = 1000;
  $('.delay-message').ajaxStart(function () {
  var $this;
  $this = $(this);
  t = setTimeout(function () {
    $this.trigger('slowajax');
  }, delay);
  }).ajaxComplete(function () {
    if (t) {
      clearTimeout(t);
    }
  });
}());

$('.delay-message').on('slowajax', function () {
  $(this).show().delay(2000).fadeOut(1000);
});

쓰임새

$.ajaxSetup({
timeout:2000 // in milliseconds 
});

//your get request here

전화를 걸어 통과합니다.timeout선택.

그냥 사용..ajax()이 일을 위하여.get()그냥 포장지일 뿐입니다다음과 같은 작업을 수행할 수 있습니다.

function doAjax() {
    var $warning = $("#warning").hide(), // Always hide to start with
        warningTimeoutId = 0;

    warningTimeoutId = window.setTimeout(function () {
         $warning.show();
    }, 10000); // wait 10s before showing warning

    $.ajax({
        timeout: 30000, // 30s timeout
        complete: function () {
            // Prevent the warning message from appearing
            window.clearTimeout(warningTimeoutId);
        }
        ... // other config
    });
}

여기서는 Ajax 요청이 아직 완료되지 않은 한 10초 후에 표시되는 Ajax 호출 직전에 타임아웃을 시작합니다.

시간 초과가 발생했을 때 사용자 지정 오류 메시지를 쉽게 표시하려면(그렇지 않으면 $.get-complete 콜백이 호출되지 않음) 다음을 사용할 수 있습니다.

$.ajaxSetup({
    timeout: 1000,
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        if (textStatus == 'timeout') {
            // timeout occured
        } else {
            // other error occured (see errorThrown variable)
        }
    }
});

언급URL : https://stackoverflow.com/questions/8535431/time-out-with-jquery-get

반응형