回答編集履歴
1
test
test
CHANGED
@@ -9,3 +9,185 @@
|
|
9
9
|
繰り返しチェックし、上限が来たらrejectすればよいと思います
|
10
10
|
|
11
11
|
後者であればサーバー設定でできると思います
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
# sample
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
よくよくテストしてみましたが
|
20
|
+
|
21
|
+
ignore_user_abort()をチェックするために出力が必要なようで、
|
22
|
+
|
23
|
+
そうとう姑息ですがこんな感じでどうでしょうか?
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
※必要なファイル
|
28
|
+
|
29
|
+
- send.htm
|
30
|
+
|
31
|
+
- recv.php
|
32
|
+
|
33
|
+
- log.txt
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
※テスト方法1
|
38
|
+
|
39
|
+
send.htmの※注1の7=最大7秒でタイムアウトする
|
40
|
+
|
41
|
+
recv.phpの※注2の5=5秒かんウェイトをかける
|
42
|
+
|
43
|
+
結果:5秒間カウントダウンして、JSONデータを受け取る
|
44
|
+
|
45
|
+
このときlog.txtには5秒間カウントダウンしたデータが出力されている
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
※テスト方法2
|
50
|
+
|
51
|
+
send.htmの※注1の7を2に書き換える=最大2秒でタイムアウトする
|
52
|
+
|
53
|
+
recv.phpの※注2の5=5秒かんウェイトをかける、そのまま
|
54
|
+
|
55
|
+
結果:2秒間カウントダウンして、errになる
|
56
|
+
|
57
|
+
このときxhrがabortされ接続は切断されるので、logには2秒分のログしか残らない
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
send.htm
|
62
|
+
|
63
|
+
```HTML
|
64
|
+
|
65
|
+
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
|
66
|
+
|
67
|
+
<script>
|
68
|
+
|
69
|
+
new Promise(function(resolver,rejector){
|
70
|
+
|
71
|
+
var timerId;
|
72
|
+
|
73
|
+
var fd=new FormData();
|
74
|
+
|
75
|
+
fd.append('x','test');
|
76
|
+
|
77
|
+
var xhr = new XMLHttpRequest();
|
78
|
+
|
79
|
+
xhr.open('POST','recv.php');
|
80
|
+
|
81
|
+
xhr.addEventListener("load" ,function() {
|
82
|
+
|
83
|
+
clearInterval(timerId);
|
84
|
+
|
85
|
+
return resolver(this);
|
86
|
+
|
87
|
+
});
|
88
|
+
|
89
|
+
var count=7;//※注1
|
90
|
+
|
91
|
+
timerId=setInterval(function(){
|
92
|
+
|
93
|
+
console.log(count);
|
94
|
+
|
95
|
+
count--;
|
96
|
+
|
97
|
+
if(count==0){
|
98
|
+
|
99
|
+
clearInterval(timerId);
|
100
|
+
|
101
|
+
xhr.abort();
|
102
|
+
|
103
|
+
return rejector(this);
|
104
|
+
|
105
|
+
}
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
},1000);
|
110
|
+
|
111
|
+
xhr.send(fd);
|
112
|
+
|
113
|
+
}).then(function(xhr){
|
114
|
+
|
115
|
+
console.log(JSON.parse(xhr.response));
|
116
|
+
|
117
|
+
console.log("end");
|
118
|
+
|
119
|
+
}).catch(function(){
|
120
|
+
|
121
|
+
console.log("err");
|
122
|
+
|
123
|
+
});
|
124
|
+
|
125
|
+
</script>
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
```
|
130
|
+
|
131
|
+
recv.php
|
132
|
+
|
133
|
+
```
|
134
|
+
|
135
|
+
<?PHP
|
136
|
+
|
137
|
+
ignore_user_abort(false);
|
138
|
+
|
139
|
+
set_time_limit(1);
|
140
|
+
|
141
|
+
set_error_handler('myErrorHandler');
|
142
|
+
|
143
|
+
function myErrorHandler($errno, $errstr){
|
144
|
+
|
145
|
+
if($errno==E_NOTICE and preg_match("/^ob_end_clean/",$errstr)){
|
146
|
+
|
147
|
+
return true;
|
148
|
+
|
149
|
+
}else{
|
150
|
+
|
151
|
+
return false;
|
152
|
+
|
153
|
+
}
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
file_put_contents('log.txt','');
|
158
|
+
|
159
|
+
for ( $i=0 ; $i<5; $i++){ //※注2:
|
160
|
+
|
161
|
+
echo("\r");
|
162
|
+
|
163
|
+
flush();
|
164
|
+
|
165
|
+
try{
|
166
|
+
|
167
|
+
ob_end_clean();
|
168
|
+
|
169
|
+
}catch(exception $e){
|
170
|
+
|
171
|
+
print $e->getMessage();
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
file_put_contents('log.txt',"$i..".connection_aborted()."(".connection_status().")".PHP_EOL,FILE_APPEND);
|
178
|
+
|
179
|
+
sleep(1);
|
180
|
+
|
181
|
+
}
|
182
|
+
|
183
|
+
print json_encode($_POST);
|
184
|
+
|
185
|
+
?>
|
186
|
+
|
187
|
+
```
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
log.txt:空ファイル
|
192
|
+
|
193
|
+
|