AJAX 요청을 지연하여 입력 셀의 마지막 키 업 이후 2-3 초 내에 전송하려고합니다.
지금까지 요청을 지연시킬 수 있었지만 2 ~ 3 초 후에는 필드의 모든 키 입력에 대해 하나의 요청을 보냈습니다 ...
어떻게 jQuery를 취소하고 첫 번째 키 업을 보낼 수 있습니까?
다음은 지금까지의 코드입니다 : 위의jQuery AJAX 요청에 지연을 추가하려고 시도했습니다.
$('#lastname').focus(function(){
$('.terms :input').val(""); //clears other search fields
}).keyup(function(){
caps(this); //another function that capitalizes the field
$type = $(this).attr("id"); // just passing the type of desired search to the php file
setTimeout(function(){ // setting the delay for each keypress
ajaxSearchRequest($type); //runs the ajax request
}, 1000);
});
이 코드는, 다음 1 초를 기다립니다 키 누름에 따라 4-5 AJAX 요청을 보냅니다. 난 단지 하나를 마지막으로 보낸 후 keyup
StackOverflow에서 Javascript를 사용하는 몇 가지 유사한 솔루션을 찾았지만 프로그래밍에 대한 나의 작은 지식으로 인해이를 프로젝트에 구현할 수 없었습니다. 당신이 할 수 있도록,
function ajaxSearchRequest($type){
var ajaxRequest2; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest2 = new XMLHttpRequest();
}catch (e){
// Internet Explorer Browsers
try{
ajaxRequest2 = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e) {
try{
ajaxRequest2 = new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){
// Something went wrong
alert("Browser error!");
return false;
}
}
}
ajaxRequest2.onreadystatechange = function(){
if(ajaxRequest2.readyState == 4){
$result = ajaxRequest2.responseText;
$('#resultcontainer').html($result);
}}
var searchterm = document.getElementById($type).value;
var queryString ="?searchterm=" + searchterm +"&type=" +$type;
if(searchterm !== ""){
ajaxRequest2.open("GET", "searchrequest.php" +
queryString, true);
ajaxRequest2.send(null);
}
}
'ajaxSearchRequest' 코드를 게시하십시오 – Imdad
@Imdad 왜 그게 필요한가요? 이전의 키 업을 취소하지 않고 각 키 업이 새로운 시간 초과를 이미 볼 수 있습니다. – Esailija
다른 해결 방법이 있습니다. 나는 함수가 무엇인지 알 수 있는지 만 알 수있다. – Imdad