轮询

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
     
    <script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
</head>
<body>

<div id="msg"></div>

</body>
</html>

<script>
    setInterval('getdata()',5000);//轮询执行,500ms一次
    // getdata();
    function getdata(){
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "t.php",
            timeout: 80000,     //ajax请求超时时间80秒
            // data: {time: "40"}, //40秒后无论结果服务器都返回数据
            success: function (data, textStatus) {
//从服务器得到数据,显示数据并继续查询
                if (data.success == "1") {
                    $("#msg").append("<br>[有数据]" + data.text);
                    evdata.data.btn.click();
                }
//未从服务器得到数据,继续查询
                if (data.success == "0") {
                    $("#msg").append("<br>[无数据]");
                    getdata();
                }
            },
//Ajax请求超时,继续查询
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                if (textStatus == "timeout") {
                    $("#msg").append("<br>[超时]");
                    evdata.data.btn.click();
                }
            }

        });
    }



</script>
<?php

echo json_encode(['success'=>1,'text'=>'datadatadatadatadatadatadatadatadatadatadatadatadata']);die;



?>
posted @ 2021-04-26 21:19  猖狂的小山猪  阅读(55)  评论(0编辑  收藏  举报