计算console.log对象 [英] Count console.log objects

查看:90
本文介绍了计算console.log对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码将控制台日志打印到页面上。它记录来自服务器的获取和响应,如:

The code below prints the console log onto the page. It logs gets and responses from server like:

14:15:17 GMT+0000 (GMT Standard Time) Submitting HTTP GET request to http...
14:15:22 GMT+0000 (GMT Standard Time) Received HTTP response: {..
14:15:17 GMT+0000 (GMT Standard Time) Submitting HTTP GET request to http...
14:15:22 GMT+0000 (GMT Standard Time) Received HTTP response: {..

我没有在页面上显示这些内容,而是希望计算每个响应和请求,以便您看到一个从1开始到结束时的数字,可以是任何数字。这是为了向用户显示正在发生的事情而没有向他们显示所有响应并获取数据。

Instead of displaying these onto the page I would like to count every response and request so you see a a number starting at 1 and ending when it ends, could be any number. This is to show the user that something is happening without showing them all the response and get data.

        function logHttpResponse(response) {
        var now = new Date();
        var logger = document.getElementById('log');
        var logMessage = now.toTimeString() + " Received HTTP response: " + JSON.stringify(response);
        console.log = function (logMessage) {
            if (typeof logMessage == 'object') {
                logger.innerHTML += (JSON && JSON.stringify ? JSON.stringify(logMessage) : String(logMessage)) + '<br />';
            } else {
                logger.innerHTML += logMessage + '<br />';
            }
        }
    }

和html:

<div id="log"></div>


推荐答案

如果您只想覆盖 console.log 打印响应计数,这应该这样做,但这会增加任何 console.log 调用的计数。 / p>

If you just want to override console.log to print the count of responses, this should do it, but this will increment the count for any console.log call.

var logCount = 0

console.log = function (logMessage) {
    var logger = document.getElementById('log');
    logCount++;
    logger.innerHTML = logCount;
}

如果您想依靠响应而不是所有控制台日志,请使用类似这个

If you want to count on responses and not all console logs, use something like this

var logCount = 0

function logHttpResponse(response) {
    var logger = document.getElementById('log');
    logCount++;
    logger.innerHTML = logCount;
}

这篇关于计算console.log对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆