JS之 定时轮询请求方法

页面

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
  获取分类
<body>

<!-- <script src="./ajax.js"></script> -->
<script>
    let page = 1 // 初始的 page 值

    let timer = setInterval(() => {
        var xhr = new XMLHttpRequest()

         var url = "https://api-hmugo-web.itheima.net/api/public/v1/goods/search"

        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) { // 请求已完成
                if (xhr.status !== 200) { // 请求失败
                    clearInterval(timer) // 清除定时器
                } else { // 请求成功
                    page += 1 // 递增 page 的值
                }
            }
        }

        // categoryids = ['8a8991f94fba0a9b014fba0b67a90002','8a8991f94fba0a9b014fba0b67a90006','8a8991f94fba0a9b014fba0b67a90007','8a8991f94fba0a9b014fba0b67a90009','8a8991f94fba0a9b014fba0b67a90008','8a8991f94fba0a9b014fba0b67a90011','8a8991f94fba0a9b014fba0b67a90014'];


        xhr.open("GET", url + "?page=" + page +'&categoryid=8a8991f94fba0a9b014fba0b67a90006'+"&page="+page, true)

        xhr.send()
    }, 1000)

</script>
</body>

</html>

ajax.js

/* See license.txt for terms of usage */

var _ = require('underscore');

exports.get = function(url, params, cb) {
    exports.send(url, 'GET', params, cb);
}

exports.post = function(url, params, cb) {
    exports.send(url, 'POST', params, cb);
}

exports.send = function(url, method, params, cb) {
    var xhr = new XMLHttpRequest();
    xhr.open(method, url, true);
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            var data = xhr.responseText;
            try {
                data = JSON.parse(data);
            } catch (exc) {
            }
            if (cb) {
                cb(data);
            }
        }
    }

var body;
if (params) {
    var bodies = [];
    for (var name in params) {
        bodies.push(name + '=' + encodeURIComponent(params[name]));
    }

    body = bodies.join('&');
    if (body.length) {
        xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
    }        
}

xhr.send(body);
}

exports.getJSON = function(url, params, cb) {
   var pairs = ['callback=jsonp'];
    _.each(params, function(value, key) {
        pairs[pairs.length] = key+'='+value;
    });
    if (pairs.length) {
        url = url + (url.indexOf('?') == -1 ? '?' : '&') + pairs.join('&');
    }

function jsonpReturn(o) {
    self.jsonp = undefined;
    if (!o || o.error) {
        if (cb) cb(o);        
    } else {
        if (cb) cb(0, o);
    }        
}

if (has('appjs')) {
    self.jsonp = jsonpReturn;

    appjs.load(url, 'GET', {}, params, function(err, data) {
        if (err) {
            cb(err);            
        } else {
            sandboxEval(data);
        }
    });
} else if (self.document) {
    self.jsonp = function(o) {
        // Return on a timeout to ensure that getJSON calls return asynchronously. There
        // is a case in IE where, after hitting the back button, this will return
        // synchronously and potentially confuse some clients.
        setTimeout(function() { jsonpReturn(o) }, 0);
    }

    function cleanup() {
        if (script.parentNode) {
            script.parentNode.removeChild(script);
        }            
    }

    var script = document.createElement('script');
    script.type = 'text/javascript';
    // script.async = true;
    script.src = url;
    script.onload = cleanup;
    script.onerror = function(event) {
        cleanup();
        cb("Error");
    };
    var head = document.getElementsByTagName("head")[0];
    head.appendChild(script);
} else {
    self.jsonp = jsonpReturn;

    var xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            eval(xhr.responseText);
            self.jsonp = null;
        }
    }
    xhr.send("");
}
}

exports.postJSON = function(url, params, cb) {
    exports.post(url, params, function(data) {
        var result = eval(data);
        cb(0, result);
    }); 
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值