문제

이 jQuery 함수에 1초의 지연을 넣어 보안 문자가 올바르게 채워졌을 때 충분한 지연(1초 정도)이 있도록 해야 합니다. #captchaStatus 프로세스의 다음 단계(게시되는 HTML) 전에 표시합니다.

jQuery를 배우고 있지만 꽤 쉬울 것입니다 ....아이디어?감사해요.

function validateCaptcha()
{
    challengeField = $("input#recaptcha_challenge_field").val();
    responseField = $("input#recaptcha_response_field").val();
    //alert(challengeField);
    //alert(responseField);
    //return false;
    var html = $.ajax({
    type: "POST",
    url: "/wp-content/themes/default/ajax.recaptcha.php",
    data: "recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField,
    async: false
    }).responseText;

    if(html == "success")
    {
        $("#captchaStatus").html("You got it right - I'm sending the email now....");
        return true;
    }
    else
    {
        $("#captchaStatus").html("Your captcha is incorrect - please try again...");
        Recaptcha.reload();
        return false;
    }
}
도움이 되었습니까?

해결책

콜백을 사용해야 합니다.확인해 보세요 이에 대한 jQuery FAQ 항목입니다.

비동기 프로그래밍의 특성상 코드가 원하는 대로 작동하지 않습니다.제공된 성공 핸들러는 즉시 호출되지 않고 나중에 서버로부터 응답이 수신될 때 호출됩니다.따라서 $.ajax 호출 직후 'status' 변수를 사용하면 해당 값은 여전히 ​​정의되지 않습니다.

 getUrlStatus('getStatus.php', function(status) {
    alert(status);
 });


function getUrlStatus(url, callback) {
  $.ajax({
     url: url,
     complete: function(xhr) {
         callback(xhr.status);
     }
  });
}

다른 팁

1 초에서 1 초에서 1 초에서 불투명성과 같은 양성 속성을 애니메이션하고 완료 콜백에서 양식을 제출함으로써 지연을 속일 수 있습니다.

$("#captchaStatus")
    .html("You got it right - I'm sending the email now....")
    .animate({opacity: 1.0}, 1000, "linear", function() {
        submitMyForm();
    });

비밀은 비 JS 기능 Settimeout입니다. 이렇게하면 1 초 후에 작업이 수행됩니다.

setTimeout(function() {
  $("#captchaStatus").html("You got it right - I'm sending the email now....");
}, 1000);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top