我正在使用 phonegap 开发一个应用程序。在我的电脑上一切都运行良好,但在我的移动设备上却太慢了。
我认为问题出在显示功能上,android 浏览器似乎需要很长时间才能隐藏和显示元素
有什么可以改进的?
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)
}
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>
...
最佳答案
虽然它可能不会对台式机产生影响,但在正确位置大量缺少分号可能会对移动设备产生影响。
JavaScript 引擎必须遍历并尝试找出分号需要去的地方。参见 this transcript from the ECMAScript specification .
老实说,我认为这只节省了几毫秒的时间,但它是现在的起点 - 也是 future 的良好实践。
关于javascript - 如何使以下代码更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6173884/