1515 < div id ="resultSection " style ="margin-top: 30px; border-top: 1px solid #eaeaea; ">
1616 </ div >
1717</ div >
18- < script charset ="utf-8 " >
18+ < script charset ="utf-8 ">
19+ let isRequesting = true ;
20+
21+ window . addEventListener ( "keydown" , ( event ) => {
22+ if ( event . key === 'Escape' ) {
23+ isRequesting = false ;
24+ }
25+ } ) ;
26+
1927 $ ( '#btnStart' ) . click ( ( ) => {
20- const url = $ ( '#inputArea' ) . val ( ) ;
21- $ ( '#resultSection' ) . empty ( ) ;
22- fetchVal ( url ) ;
28+ const url = $ ( '#inputArea' ) . val ( ) ;
29+ $ ( '#resultSection' ) . empty ( ) ;
30+ isRequesting = true ;
31+ fetchVal ( url ) ;
2332 } ) ;
2433
2534 const getDateTime = ( ) => {
26- const myDate = new Date ;
27- const year = myDate . getFullYear ( ) ; //获取当前年
28- const month = myDate . getMonth ( ) + 1 ; //获取当前月
29- const date = myDate . getDate ( ) ; //获取当前日
30- const hours = myDate . getHours ( ) ;
31- const minutes = myDate . getMinutes ( ) ;
32- const seconds = myDate . getSeconds ( ) ;
33- return `${ year } -${ month } -${ date } ${ hours } :${ minutes } :${ seconds } ` ;
35+ const myDate = new Date ;
36+ const year = myDate . getFullYear ( ) ; //获取当前年
37+ const month = myDate . getMonth ( ) + 1 ; //获取当前月
38+ const date = myDate . getDate ( ) ; //获取当前日
39+ const hours = myDate . getHours ( ) ;
40+ const minutes = myDate . getMinutes ( ) ;
41+ const seconds = myDate . getSeconds ( ) ;
42+ return `${ year } -${ month } -${ date } ${ hours } :${ minutes } :${ seconds } ` ;
3443 } ;
3544
3645 const fetchVal = ( url ) => {
37- $ . ajax ( url , {
46+ $ . ajax ( url , {
3847
39- success : ( data ) => {
40- $ ( '#resultSection' ) . append ( `<p>${ getDateTime ( ) } 返回 ${ data } </p>` ) ;
41- setTimeout ( ( ) => {
42- fetchVal ( url ) ;
43- } , 500 ) ;
44- } ,
45- error : ( errorThrown ) => {
46- $ ( '#resultSection' ) . append ( `<p>${ getDateTime ( ) } 返回 ${ errorThrown . responseText } </p>` ) ;
47- setTimeout ( ( ) => {
48- fetchVal ( url ) ;
49- } , 500 ) ;
50- } ,
51- } ) ;
48+ success : ( data ) => {
49+ $ ( '#resultSection' ) . append ( `<pre>${ getDateTime ( ) } 返回 ${ data } </pre>` ) ;
50+ if ( isRequesting ) {
51+ setTimeout ( ( ) => {
52+ fetchVal ( url ) ;
53+ } , 500 ) ;
54+ }
55+ } ,
56+ error : ( errorThrown ) => {
57+ $ ( '#resultSection' ) . append ( `<pre>${ getDateTime ( ) } 返回 ${ errorThrown . responseText } </pre>` ) ;
58+ if ( isRequesting ) {
59+ setTimeout ( ( ) => {
60+ fetchVal ( url ) ;
61+ } , 500 ) ;
62+ }
63+ } ,
64+ } ) ;
5265 } ;
53- </ script >
66+ </ script >
5467</ body >
5568</ html >
0 commit comments