Pass the function ... :)
Using an anonymous function may be as follows:
var timeoutId
function autoComplete(q, succ)
{
if (q) {
clearTimeout(timeoutId)
timeoutId = setTimeout(function () {
$.ajax({type:"GET",
url: "php/search.php",
data: "q="+q,
success: succ
});
}, 1000);
}
}
. q . , . , success - - . " q" q setTimeout .
var timeoutId
var counter = 0
function autoComplete(q, succ)
{
if (q) {
counter++
var thisCounter = counter
clearTimeout(timeoutId)
timeoutId = setTimeout(function () {
$.ajax({type:"GET",
url: "php/search.php",
data: "q="+q,
success: function () {
if (counter == thisCounter) {
succ.apply(this, arguments)
}
},
});
}, 1000);
}
}
, ...
getQ ...
var timeoutId
var counter = 0
function autoComplete(getQ, succ)
{
counter++
var thisCounter = counter
clearTimeout(timeoutId)
timeoutId = setTimeout(function () {
var q = getQ()
if (q) {
$.ajax({type:"GET",
url: "php/search.php",
data: "q="+q,
success: function () {
if (counter == thisCounter) {
succ.apply(this, arguments)
}
},
});
}
}, 1000);
}
autoComplete(function () { return $(elm).val() }, successCallback)
.
, , , , ( , "" , ). AJAX , "-", .
user166390