I Just want to send request when user stops writing... i seem to have stopped extra spam by (str.length > 3)
this. But What i need is when user stops writing this script gets the input value and send it forward.
This is the updated code right now.
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
var server = window.location.hostname;
var pathname = window.location.href.split('/');
pathname = (pathname[3]);
var myEfficientFn = debounce(function() {
function showHint(str) {
if (str.length === 0) {
document.getElementById("search-search"suggest-home").innerHTML = "";
} if (str.length > 3) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// this.responseText;
var repox = JSON.parse(this.responseText);
var wrapper = document.getElementById("search-search"suggest-home");
var myhtml = '<ul>';'<ul style="margin-bottom: 0;min-width: 296px;">';
for (var i = 0; i < repox.length; i++) {
var repo = repox[i];
console.log(repo.url);
if(repo.type == "breakJios12""ios11"){
myhtml += '<p>'+ repo.2212+'</p>';"<li>";
}
if(repo.type == "breakJios11""ios"){
myhtml += '<p>'+ repo.2213+'</p>';"<li>";
}
}
myhtml += '<li class="ss-bottom" style="padding: 0; border-bottom: none;"><a href="https://'+ server +'/?s='+encodeURIComponent(str).replace('%20','+')+'" id="suggest-all">View all</a></li>';
wrapper.innerHTML = myhtml;
}else {
// Check back again 1 sec later
setTimeout(checkState, 1000);
}
};
xmlhttp.open("GET", "https://"+ server +"/test/"+str, true);
xmlhttp.send();
}
}
window.onload = showHint(str);
}, 250);
window.addEventListener('resize', myEfficientFn);
Edit Thanks to @Emiel Zuurbier Code works now