w
sleep(10);改为sleep(1);
w用sleep模拟掉线
error---异常exception,是否支付成功不确定
<?php sleep(10); if (isset($_REQUEST[‘sleep‘])) { echo json_encode(array(‘code‘ => 1, ‘message‘ => ‘request‘)); die; }else{ echo json_encode(array(‘code‘ => 0, ‘message‘ => ‘request_no‘)); die; }
CODE hank1.php <script src="static/jquery-3.1.0.min.js"></script> <script> var z = currentTime(); var flag = 1; var long_time = 2000; setInterval(function () { var c = currentTime() - z; if (c > 10 && flag) { flag = 0; } }, 500); $.post(‘hank2.php‘, {sleep: ‘hank_v‘}, function (data) { var c = currentTime(); var interval = c - z if (interval < long_time) { console.log(‘ok‘); ajax_back(data) } else { error(‘error‘); } }); function ajax_back(data) { console.log(data); } function error(data) { console.log(data) } function currentTime() { var d = new Date(); return d.getTime(); } </script>