Home Break the for loop based on the ajax success variable
Reply: 1

Break the for loop based on the ajax success variable

Steffi
1#
Steffi Published in 2017-11-13 11:14:52Z

For the following code, the emailCnt is 50 for first iteration, I need 25 in next iteration. What is the possible way to access the variable value outside the ajax success and break the for loop execution?

var limit = 50;
var emailCnt = limit;
for (var i = 0; i < 20; i++) {
    console.log(emailCnt);///this value is 50 instead I need 25     
    if (emailCnt < limit && i != 0) {
        break;
    }
    setTimeout(function () {
        submit_post(slNo, limit, function (output) {
            slNo = output;
            emailCnt = 25;
            $('#load_data').html('Hello');
        });
    }, 1000);
}
function submit_post(slNo, limit, handleData) {
    $.ajax({
        type: 'POST',
        async: false,
        url: url,
        data: { slNo: slNo, limit: limit },
        success: function (data) { handleData(data); }
    });
}
Steffi
2#
Steffi Reply to 2017-11-13 13:28:31Z

This successfully worked for me

var limit           =   50;
var emailCnt        =   limit;
function submit_post(slNo, limit) 
{
    var result="";
    $.ajax({
        type: 'POST',
        async: false,
        url: url,
        data: {slNo:slNo, limit:limit},
        success: function(data) { result = data; }
    });
    return result;
}
for(var i=0;i<20;i++)
{
    if(emailCnt     <   limit  &&  i  !=  0)
    {
        break;
    }
    setTimeout(function () {
    var output              =   submit_post(slNo, limit); 
    slNo                    =   output;
    emailCnt                =   25;
    $('#load_data').html('Hello');
}, 1000);
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.297835 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO