0
以下腳本通過每秒發出兩次ajax請求(使用setInterval和500 ms超時)來更新進度欄。我添加了isBusy標誌,以便在預先存在的請求尚未收到響應時不發出ajax請求。但它不起作用。當然,當我註釋掉第19行,這是我設置標誌以忙於等待響應的地方,所有工作都可以。我究竟做錯了什麼?我如何做到這一點,一次只有1個Ajax請求是「活着的」,並等待響應?限制Ajax進度條
01 var exportHandler = {
02
03 handlerUrl: '',
04 intervalId: 0,
05 isBusy: 0,
06
07 start: function() {
08
09 // do some set up work
10
11 exportHandler.isBusy = 0; // init flag to not busy, not waiting on resp
12
13 exportHandler.intervalId = setInterval(function() {
14
15 if (exportHandler.isBusy == 1) {
16 return; // response to previous request not yet rcvd
17 }
18
19 exportHandler.isBusy = 1; // set flag to busy waiting on response
20
21 exportHandler.getProgress();
22 }, 500);
23 },
24
25 cancel: function() {
26 exportHandler.isBusy = 1; // not sure if I need this here???
27
28 if (exportHandler.intervalId != 0) {
29 clearInterval(exportHandler.intervalId);
30 }
31
32 exportHandler.intervalId = 0;
33 event.preventDefault();
34 return false;
35 },
36
37 getProgress: function() {
38
39 $.ajax({
40 url: exportHandler.handlerUrl,
41 dataType: 'json',
42 async: true,
43 data: ...,
44
45 error: function (o, st, err) {
46 exportHandler.isBusy = 0; // err occured so reset flag to not busy waiting on response
47
48 // handle error
49 },
50
51 success: function (job) {
52 exportHandler.isBusy = 0; // response rcvd so reset flag to not busy waiting on response
53
54 showProgress(job, exportHandler);
55
56 if (job.JobComplete) {
57 jobsDone(exportHandler.intervalId, 'export');
58 }
59 }
60 });
61 }
62
63 }