前台:
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="jquery-3.4.1.js"></script>
</head>
<body>
<button>发送请求</button>
<script>
var btn=document.querySelector("button");
btn.onclick=function()
{
var xhr=new XMLHttpRequest();
xhr.onreadystatechange=function()
{
if(xhr.readyState==4)
{
if(xhr.status==200)
{
console.log(xhr.responseText);
}
}
}
//请求超时设置
xhr.timeout=5000;
xhr.ontimeout=function(){
console.log('连接超时');
}
xhr.open('get','3.php',true);
xhr.send(null);
}
</script>
</body>
</html>
后台:
sleep(秒数); 停止当前进程指定秒数
<?php
sleep(10);
$success=array('mes'=>'ok');
echo json_encode($success);
?>