मैं इतना है कि यह एक इनपुट सेल के पिछले KeyUp के बाद बाहर भेज दिया जाता 2-3 सेकंड एक AJAX अनुरोध देरी करने के लिए कोशिश कर रहा हूँ करने के लिए देरी जोड़ने के लिए कोशिश कर रहा है।
अब तक मैं अनुरोध में देरी करने में कामयाब रहे, लेकिन 2-3 सेकंड के बाद मैं एक अनुरोध क्षेत्र में हर KeyUp के लिए भेजे जाते हैं ...
मैं jQuery सबसे पहले रद्द कर देगा और अभी पिछले KeyUp भेज कैसे बना सकते हैं?
यहाँ अब तक कोड है: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 keypresses के आधार पर अनुरोध भेजता है। मैं सिर्फ अंतिम keyup
के बाद भेजा गया एक चाहता हूं मुझे स्टैक ओवरव्लो में कुछ समान समाधान मिल गए हैं जो जावास्क्रिप्ट का उपयोग करते हैं, लेकिन मैं प्रोग्रामिंग के अपने छोटे ज्ञान के कारण उन्हें अपने प्रोजेक्ट में लागू करने में सक्षम नहीं हूं।
[हल] अंतिम काम कर कोड, @ Dr.Molle करने के लिए धन्यवाद:
$('#lastname').focus(function(){
$('.terms :input').val("");
}).keyup(function(){
caps(this);
$type = $(this).attr("id");
window.timer=setTimeout(function(){ // setting the delay for each keypress
ajaxSearchRequest($type); //runs the ajax request
}, 3000);
}).keydown(function(){clearTimeout(window.timer);});
यहाँ ajaxSearchRequest
कोड है:
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