当前位置: 首页 > 未分类>阅读正文

ajax计算请求时间

2022.7.17 朱丰华 1135 次 留下评论 500字

设置全局变量start_time

在 beforeSend 钩子设置start_time为当前时间

然后在 complete 钩子取得结束时间,并算出时间差。

<script src="https://www.52dixiaowo.com/tools/npm/jquery@3.6.0/dist/jquery.min.js"></script>

<script>
    $.ajaxSetup({
        beforeSend: function() {
            $.start_time = new Date().getTime();
        },
        complete: function() {
            $.lastRequestTime = new Date().getTime() - $.start_time;
            console.log("本次请求花费时间:" + $.lastRequestTime + "ms");
            document.write("本次请求花费时间:" + $.lastRequestTime + "ms");
        }
    });

    $.get("https://autumnfish.cn/api/joke/list?num=1", {}, function(r) {}, "json");
</script>

本篇完,还有疑问?留下评论吧

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注