Skip to content

Commit ae4c29c

Browse files
implement ajax delay before send request
1 parent 946f7e7 commit ae4c29c

File tree

1 file changed

+39
-21
lines changed

1 file changed

+39
-21
lines changed

src/autocomplete.ts

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010

1111
interface Params {
1212
// Custom params
13+
Delay: number;
1314
EmptyMessage: string;
1415
HttpHeaders: Object;
15-
Limit: number;
1616
HttpMethod: string;
17+
Limit: number;
1718
QueryArg: string;
1819
Url: string;
1920

@@ -31,7 +32,7 @@ interface Params {
3132
_EmptyMessage: any;
3233
_Focus: any;
3334
_Limit: any;
34-
_HttpMethod: any;
35+
_HttpMethod: any;
3536
_Open: any;
3637
_QueryArg: any;
3738
_Position: any;
@@ -40,6 +41,9 @@ interface Params {
4041
_Pre: any;
4142
_Select: any;
4243
_Url: any;
44+
45+
// Internal item
46+
$AjaxTimer: WindowTimers;
4347
}
4448

4549
interface MappingCondition {
@@ -83,6 +87,7 @@ class AutoComplete {
8387
return merge;
8488
};
8589
static defaults: Params = {
90+
Delay: 150,
8691
EmptyMessage: "No result here",
8792
HttpHeaders: {
8893
"Content-type": "application/x-www-form-urlencoded"
@@ -361,6 +366,8 @@ class AutoComplete {
361366
this.Select.appendChild(option);
362367
}
363368
},
369+
370+
$AjaxTimer: null,
364371
};
365372

366373
// Constructor
@@ -469,31 +476,42 @@ class AutoComplete {
469476
};
470477
}
471478

472-
ajax(params: Params, callback: any): void {
473-
console.log("AJAX", params);
474-
if (params.Request) {
475-
params.Request.abort();
479+
ajax(params: Params, callback: any, timeout: boolean = true): void {
480+
if (params.$AjaxTimer) {
481+
window.clearTimeout(params.$AjaxTimer);
476482
}
477-
478-
var propertyHttpHeaders = Object.getOwnPropertyNames(params.HttpHeaders),
479-
method = params._HttpMethod(),
480-
url = params._Url(),
481-
queryParams = params.QueryArg + "=" + params._Pre();
482483

483-
if (method.match(/^GET$/i)) {
484-
url += "?" + queryParams;
485-
}
484+
if (timeout == true) {
485+
console.log("AJAX Timeout");
486+
487+
params.$AjaxTimer = window.setTimeout(AutoComplete.prototype.ajax.bind(null, params, callback, false), params.Delay);
488+
} else {
489+
console.log("AJAX Sended", params);
486490

487-
params.Request = new XMLHttpRequest();
488-
params.Request.open(method, url, true);
491+
if (params.Request) {
492+
params.Request.abort();
493+
}
494+
495+
var propertyHttpHeaders = Object.getOwnPropertyNames(params.HttpHeaders),
496+
method = params._HttpMethod(),
497+
url = params._Url(),
498+
queryParams = params.QueryArg + "=" + params._Pre();
489499

490-
for (var i = propertyHttpHeaders.length - 1; i >= 0; i--) {
491-
params.Request.setRequestHeader(propertyHttpHeaders[i], params.HttpHeaders[propertyHttpHeaders[i]]);
492-
}
500+
if (method.match(/^GET$/i)) {
501+
url += "?" + queryParams;
502+
}
493503

494-
params.Request.onreadystatechange = callback;
504+
params.Request = new XMLHttpRequest();
505+
params.Request.open(method, url, true);
495506

496-
params.Request.send(queryParams);
507+
for (var i = propertyHttpHeaders.length - 1; i >= 0; i--) {
508+
params.Request.setRequestHeader(propertyHttpHeaders[i], params.HttpHeaders[propertyHttpHeaders[i]]);
509+
}
510+
511+
params.Request.onreadystatechange = callback;
512+
513+
params.Request.send(queryParams);
514+
}
497515
}
498516

499517
destroy(params: Params): void {

0 commit comments

Comments
 (0)