简体   繁体   English

如何使以下代码更快?

[英]how to make the following code faster?

I am developing a application with phonegap.我正在开发一个带有 phonegap 的应用程序。 on my pc everything runs fine but on my mobile device its just too slow.在我的电脑上一切正常,但在我的移动设备上它太慢了。

i think the problem is on the show function, the android browser seems to needs really long to hide and show elements我认为问题出在节目 function 上,android 浏览器似乎需要很长时间才能隐藏和显示元素

what can be improved?有什么可以改进的?

function show(id){
    $('.view').hide()
    //alert('show ' + id)
    $('#'+id+'View').show()
    scroll(0,0)
}


function getSoundHTML(id, myname, del){

    if (del != true){
        var imgsrc = 'plus.png'
        var f = function(){
            addToCostumSounds(id)
            alert('added to favorites')
        }
    }else{
        var imgsrc = 'minus.png'
        var f = function(){
            removeFromCostumSounds(id);
            $(this).fadeOut().next('div').fadeOut();
        }
    }

    var div = $('<div></div>').addClass('box').html(myname).css('border-color', '999999').click(function(){
        play(id)
    })
    var img = $('<img></img>').attr('src', imgsrc).addClass('sideimg').click(f)

    return $('<div></div>').append(img).append(div)
}

for(var category in categories){

    var f = function(category){
        $('#'+category+'Btn').click(function(){
                show(category)

                var categoryView = $('#'+category+'View')
                for(var key in categories[category]){
                    var div = getSoundHTML(key, categories[category][key])
                    categoryView.prepend(div)
                }
                var img = '<img src="menu.png" class="menuimg"/>'
                categoryView.prepend(img)
                categoryView.append(img)
        })
    }
    f(category)
}

the html: html:

    <div class="btn" id="noBtn">no _</div>
    <div class="btn" id="thatIsBtn">that is _</div>
    <div class="btn" id="thereIsBtn">there is _</div>
    <div class="btn" id="thisIsBtn">this is _</div>
    ...


<div class="view" id="noView"></div>
<div class="view" id="thatIsView"></div>
<div class="view" id="thereIsView"></div>
<div class="view" id="thisIsView"></div>
...

Whilst it may not have an effect on Desktops, your massive lack of semi-colons in the right places may have an effect on mobile devices.虽然它可能对台式机没有影响,但您在正确位置大量缺少分号可能会对移动设备产生影响。

The JavaScript engine has to run through and try to work out where the semi-colons need to go. JavaScript 引擎必须运行并尝试找出分号需要 go 的位置。 See this transcript from the ECMAScript specification .请参阅ECMAScript 规范中的此记录

To be honest I think thats only a few milliseconds of time-saved, but its a starting point for now - and good practice for the future.老实说,我认为这只是节省了几毫秒的时间,但它是现在的起点——也是未来的良好实践。

Here's your problem:这是你的问题:

for(var category in categories){

    var f = function(category){
        ...
        for(var key in categories[category])
        ...
    }

    f(category)
}

You have two BIG issues here:您在这里有两个大问题:

  1. You're defining a function within a loop.您正在循环中定义 function 。 While this is sometimes needed, you should do your very best to avoid defining things within a loop unless you absolutely need to.虽然有时需要这样做,但除非绝对需要,否则您应该尽最大努力避免在循环中定义事物。 In this case, you could probably move the f function out of the loop entirely without breaking your code.在这种情况下,您可以将f function 完全移出循环而不会破坏您的代码。
  2. Nested loops.嵌套循环。 You have a for... in within a for... in loop.您在for... in循环中有一个for... in ... in 。 This is largely due to the first problem I pointed out, but in general nested loops are a big no-no from a performance standpoint.这主要是由于我指出的第一个问题,但从性能的角度来看,嵌套循环通常是一个很大的禁忌。

ok, i think i got the only way to improve the peroformance: if someone clicks on a button (class="btn") he is redirected to a new fresh page that has the entire page in HTML and does not build it with javascript.好的,我认为我有唯一的方法来提高性能:如果有人点击一个按钮(class="btn"),他会被重定向到一个新的页面,该页面的整个页面都在 HTML 中并且不使用 javascript 构建它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM