-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebounce.js
More file actions
39 lines (24 loc) · 830 Bytes
/
Copy pathDebounce.js
File metadata and controls
39 lines (24 loc) · 830 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
(function readyJS(win,doc){
'use strict';
let search=doc.querySelector("#search");
function sendForm(){
let ajax= new XMLHttpRequest();
ajax.open("GET","calculadora.js");
ajax.onreadystatechange=function(){
if(ajax.status=== 200 && ajax.readyState===4){
console.log(ajax.responseText);
}
}
ajax.send();
}
//Nesta situação, cada vez que fazemos a chamada ao servidor "sendForm", ele aguarda 3segundos para fazer a proxima chamada
search.addEventListener("keyup", debounce(sendForm,3000),false);
//função que vai evitar chamadas repetidas ao servidor, isto é, assim podemos determinar o tempo entre cada chamada
function debounce(func,wait){
let timeout;
return function(){
clearTimeout(timeout);
timeout = setTimeout(func,wait);
}
}
})(window,document);