简体   繁体   English

如何使用数组和对象来加速这个慢动作的jQuery脚本?

[英]how to speed up this slow-motion jquery script with array and objects?

On my site I have a set of input buttons with sizes. 在我的网站上,我有一组带有大小的输入按钮。

// input elements
<input type="button" value="S" class="pblI" />
<input type="button" value="M" class="pblI" />
<input type="button" value="L" class="pblI" />

// output element
<input type="text" id="sizeMaster" value="" />

The user can click these buttons to construct an assortment, for example size S/1, M/2, L/3. 用户可以单击这些按钮来构建一个分类,例如大小为S / 1,M / 2,L / 3。 A click on size S adds S/1 to the assortment. 单击大小S,将S / 1添加到分类中。 Next click on S make it S/2 and so on. 下一步单击S,使其变为S / 2,依此类推。

I'm using it on a mobile site with Jquery Mobile, so I know I'm getting the 300ms delay click. 我正在Jquery Mobile的移动网站上使用它,所以我知道我得到了300毫秒的延迟点击。

Still the script is awfully slow to exectute, so I'm wondering if someone can point me to any "performance enhancements" for the following: 脚本执行起来仍然很慢,所以我想知道是否有人可以为我指出以下方面的“性能增强”:

// an array and defaults
var remSize = [],
remIndex = -1,
szString, remData, i;

// click listener
$(document).on('click', '.pblI', function () {

    // when clicked, construct a new object ala {size=S;qty=1}
    szString = "";
    remData = {};
       remData.size = $(this).find('input').val();
       remData.qty = 1;

    // loop through the array to see whether the size is already in there
    for (i = 0; i < remSize.length; i++) {
        // return index position of size (otherwise index stays on -1)
        if (remSize[i].size == remData.size) {
            remIndex = i;
            break;
        }
    }

  // if the size is not in the array push the new object into the array
  if (remIndex == -1 || typeof remIndex == "undefined") {        
    remSize.push(remData);
    } else {
       // else increase qty of exisiting size by 1          
       ++remSize[remIndex].qty;
       }

// create input string to display for the user
$(remSize).each(function (i) {
    szString = szString + remSize[i].size + "/" + remSize[i].qty + " ";
    // this will output something like this: 34/1 36/2 38/1
    });
// update input field
$('#sizeMaster').val(szString);
});

I don't know what exactly is slow, but you can do these parts a little bit faster: 我不知道到底什么慢,但是您可以更快地完成这些部分:

    for (i = 0; i < remSize.length; i++) {
        // return index position of size (otherwise index stays on -1)
        if (remSize[i].size == remData.size) {
            remIndex = i;
            break;
        }
    }

to

    for (i=0,j=remSize.length;i<j;++i) {
        // return index position of size (otherwise index stays on -1)
        if(remSize[i].size === remData.size) {
            remIndex = i; i = j;
        }
    }

and

$(remSize).each(function (i) {
    szString = szString + remSize[i].size + "/" + remSize[i].qty + " ";
    // this will output something like this: 34/1 36/2 38/1
    });

to

for(i=0;i<j;++i) {
    szString += remSize[i].size + "/" + remSize[i].qty + " ";
}

and

$('#sizeMaster').val(szString);

to

document.getElementById('sizeMaster').value = szString;

But i don't think this will make a big difference. 但是我认为这不会有很大的不同。

Edit: Of course you need to define "j" at the beginning. 编辑:当然,您需要在开始时定义“ j”。

You can remove some of the delay by listening for touchend rather than click 您可以通过监听触摸端而不是点击来消除某些延迟

Here's an example - http://code.google.com/mobile/articles/fast_buttons.html 这是一个示例-http://code.google.com/mobile/articles/fast_buttons.html

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

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