JavaScript 节流
< script>
function method ( med, api, async , data, callback) {
var http = new XMLHttpRequest ( ) ;
if ( med == "get" ) {
if ( data) {
api += "?" ;
api += data;
}
http. open ( med, api, async ) ;
http. send ( ) ;
}
else {
http. open ( med, api, async ) ;
if ( data) {
http. send ( data) ;
}
else {
http. send ( ) ;
}
}
http. onreadystatechange = function ( ) {
if ( http. readyState == 4 && http. status == 200 ) {
callback ( http. response) ;
}
}
}
function getData ( time, t) {
return function ( ) {
clearTimeout ( time) ;
time= setTimeout ( function ( ) {
method ( "post" , "./data.txt" , true , "" , function ( result) {
console. log ( result) ;
} ) ;
} , t) ;
}
}
var txt = document. getElementById ( "txt" ) ;
var time;
txt. addEventListener ( "keyup" , getData ( time, 500 ) ) ;
< / script>