如果控制台打开,Javascript运行得更快

前端之家收集整理的这篇文章主要介绍了如果控制台打开,Javascript运行得更快前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用大量的 JavaScript开发一个phonegap应用程序.现在我正在使用Safari Developer Tool对其进行调试,特别是我专注于设备上的一些按钮似乎有点懒散.
所以我添加了一些console.timeEnd()以更好地理解代码减速的位置,但“问题”是当我打开控制台时代码开始运行得更快而没有延迟,如果我再次关闭它,滞后回来.

也许我的问题很愚蠢,但我无法弄清楚

谢谢

编辑:添加代码

function scriviNumeroTastiera(tasto){
        console.time('Funzione ScriviNumeroTastiera');
        contenutoInput = document.getElementById('artInserito').value;
        if ($('#cursoreImg').css('display') == 'none'){
            //$('#cursoreImg').show();

        }
        else if (tasto == 'cancella'){

            //alert(contenutoInput.length);
            if (contenutoInput.length == 0) {

            }
            else {
                indicePerTaglioStringa = (contenutoInput.length)-1;
                contenutoInput = contenutoInput.substr(0,indicePerTaglioStringa);
                $('#artInserito').val(contenutoInput);
                //alert('tastoCanc');
                margineAttualeImg = $('#cursoreImg').css('margin-left');
                indicePerTaglioStringa = margineAttualeImg.indexOf('p');
                margineAttualeImg = margineAttualeImg.substr(0,indicePerTaglioStringa);
                margineAggiornato = parseInt(margineAttualeImg)-20;
                $('#cursoreImg').css('margin-left',margineAggiornato+'px');
            }
        }
        else {
            //contenutoInput = document.getElementById('artInserito').value;
            contenutoAggiornato = contenutoInput+tasto;
            margineAttualeImg = $('#cursoreImg').css('margin-left');
            indicePerTaglioStringa = margineAttualeImg.indexOf('p');
            margineAttualeImg = margineAttualeImg.substr(0,indicePerTaglioStringa);
            margineAggiornato = parseInt(margineAttualeImg)+20;
            $('#cursoreImg').css('margin-left',margineAggiornato+'px');
            $('#artInserito').val(contenutoAggiornato);
        }
        console.timeEnd('Funzione ScriviNumeroTastiera');
    }

代码有点糟糕,但它只是一个开始;)

解决方法

这可能是因为PhoneGap / Cordova创建了自己的控制台对象(在cordova.js中),当你打开Safari控制台时它会被覆盖(safari可能比phonegap更快,这可能就是为什么你会更快地注意到它).

因此,在不打开控制台的情况下正确测量时间的一种方法是转到旧的警报,因此您首先要在应用中的任何位置添加代码

var TIMER = {
    start: function(name,reset){
        if(!name) { return; }
        var time = new Date().getTime();
        if(!TIMER.stimeCounters) { TIMER.stimeCounters = {} };
        var key = "KEY" + name.toString();
        if(!reset && TIMER.stimeCounters[key]) { return; }
        TIMER.stimeCounters[key] = time;
    },end: function(name){
        var time = new Date().getTime();
        if(!TIMER.stimeCounters) { return; }
        var key = "KEY" + name.toString();
        var timeCounter = TIMER.stimeCounters[key];
        if(timeCounter) {
            var diff = time - timeCounter;
            var label = name + ": " + diff + "ms";
            console.info(label);
            delete TIMER.stimeCounters[key];
        }
        return diff;
    }
};

(这只是模仿console.time和console.timeEnd方法,但它返回值,以便我们可以提醒它).

然后,而不是调用

console.time('Funzione ScriviNumeroTastiera');

你打电话给:

TIMER.start('Funzione ScriviNumeroTastiera');

而不是打电话:

console.timeEnd('Funzione ScriviNumeroTastiera');

你打电话给:

var timeScriviNumeroTastiera = TIMER.end('Funzione ScriviNumeroTastiera');
alert('Ellapsed time: ' + timeScriviNumeroTastiera);

这样可以在不打开控制台的情况下为您提供适当的时间,因此它可以计算出phonegap应用中的实时时间.

希望这可以帮助.干杯

原文链接:https://www.f2er.com/js/150115.html

猜你在找的JavaScript相关文章