Wednesday, May 28, 2014

Jquery Looping Cause "long running scripts"

I have to load array variable into selection box (selectBoxIt plugin). First attempt was using $.each and it cause overhead in jquery, therefore I got error "long running scripts". It has been solved by replace $.each loop method with regular for loop method.

Solution:
$.ajax({
    url: '/GetBar',
    type: 'GET',
    dataType: 'json',
    async: true,
    success: function(data) {
      var selectBox = $("select#Bar").data("selectBox-selectBoxIt");
      var bar = [];
       for (var i=0;i<data.length;i++) {
         bar[i] = {value:data[i]['mdm_id'], text:data[i]['bar']}
       }
       selectBox.add(bar);
    }
  });

But there are more studies about performance among $.each and for loop in jquery, will check out when i free. (http://stackoverflow.com/questions/14808144/each-vs-each-vs-for-loop-in-jquery)

No comments:

Post a Comment