0

i am running an sql query in an loop . the query should run only if the loop count is equal to 500 .

for E,g

$.ajax({

    response:function(e){

    // Here e.length will be unknown

    for(var i in e){

        if(q1cnt <= 500){
            // do some appending 
            var k = k + 1 ;
            if(q1cnt == 500){
                tx.executeSql(k);           
            }
        }

    }


    }
})

In the above case i want to execute an sql operation if the count reaches 500 , my problem is if the length of response is 1344 for first two 500 it will be okay but how can i call tx.executeSql(k); at the end of 433 items.

user3383301
  • 1,891
  • 3
  • 21
  • 49

2 Answers2

0

I am still confused why one could possibly need to execute SQL query from a JS for every 500-th object. However, I possibly know how to achieve this.

It looks like you are complicating it while it can be solved without additional counters, flags etc.

Here is the cycle which will get the value by Ajax request.
Then, it executes tx.executeSql for every 500-th data item and the last one.

$.ajax({
    response: function(e)
    {
        for (var i = 0; i < e.length; i++)
        {
            if (i % 500 === 0 || i === e.length - 1)
            {
                tx.executeSql(i);           
            }
        }
    }
});

You have provided a very strange portion of Ajax call code. If it is kind of 'different' AJAX, then it's OK. However, talking about jQuery AJAX, the syntax should be the following:

$.ajax({
    // options
}).done(function(e)
    {
        for (var i = 0; i < e.length; i++)
        {
            if (i % 500 === 0 || i === e.length - 1)
            {
                tx.executeSql(i);           
            }
        }
    }
});
Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
0

Note: I don't know what this response property is in the object you're giving $.ajax; it's not in the standard options. But I assume you used it for a reason, perhaps some plugin. If not, change response to success in the below.

You've said that e.length will be "unknown." I assume you really mean "unknown," not undefined, and that e is, in fact, an array. If that's true:

$.ajax({

    response:function(e){
        var i;

        // Here e.length will be unknown

        for (i = 0; i < e.length; ++i) {
            if (i <= 500) {
                // do some appending
                if (i === 500) {
                    tx.executeSql(i);
                }
            }
        }
    }
});

If that's not true and e is a non-array object, then beware that up through ES5, object properties have no defined order and so for-in may visit them in an order you're not expecting. (In ES6, they will have an order.)

With that caveat:

$.ajax({

    response:function(e){
        var i, count;

        // Here e.length will be unknown

        count = 0;
        for (i in e) {
            if (count <= 500) {
                // do some appending
                if (count === 500) {
                    tx.executeSql(count);
                }
            }
            ++count;
        }
    }
});
Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875