0

As always, excuse my 'Tarzan' english, please :P

The issue is: I have to call a webService with time intervals to ensure that a list of contacts is uploaded completely.

The procedure is:
1.-Click on a button to send the CSV file.
2.-A div with a upload animation must bee seen WHILE UPLOADING THE CSV to the database.
3.-When the websService ends entering the data, the div with the unload animation must disappear.

I've created a JS function that ' I Think' is ok, as shown:

function loadWS(idArchive){
    $('#loaderX').css('display', 'block');            //Here starts with the animation
    var interrupt=setInterval(function(){             //Start asking the WS
    $.ajax({
        data:{
            'idArchive': idArchive,
        },
        url:'/NewsLetters.checkFinal',                //WebService call: the webservice checks 
        type:'post',                                  //if register entry is complete.
        success:function(res){
            var r=eval(res);
            if(r==1){                                 //IF Response ok? we've finished the task
                clearInterval(interrupt);
                load_sec(link,106);                   //This reloads the section via AJAX
                $('#loaderX').css('display', 'none'); //Here stops the animation
            }
            if (r==0) {
            }
        }
    });
},1000);
}

Am I doing something wrong? Is it correct to call the WS in the setInterval proc?

Thanks to all in advance!

Charles380
  • 1,269
  • 8
  • 19
aNGRoN
  • 63
  • 5

2 Answers2

1

just do the below and increase time atleast 5 seconds.

setInterval(function(){
    $.ajax({
        data:{
            'idArchive': idArchive,
        },
        url:'/NewsLetters.checkFinal',                //WebService call: the webservice checks 
        type:'post',                                  //if register entry is complete.
        success:function(res){
            var r=eval(res);
            if(r==1){                                 //IF Response ok? we've finished the task
                clearInterval(interrupt);
                load_sec(link,106);                   //This reloads the section via AJAX
                $('#loaderX').css('display', 'none'); //Here stops the animation
            }
            if (r==0) {
            }
        }
    });
}, 5000);
Ashwini Verma
  • 7,477
  • 6
  • 36
  • 56
1

Based on the checking the process you don't need a setInterval you just need to call the function again. I would just do the straight ajax call straight up. Just be careful as it could cause an infinite loop

function loadWS(idArchive) {
    $('#loaderX').css('display', 'block'); //Here starts with the animation
    checkStatus(idArchive);
}

function checkStatus(idArchive) {
    $.ajax({
        data: {
            'idArchive': idArchive
        },
        url: '/NewsLetters.checkFinal', //WebService call: the webservice checks 
        type: 'post', //if register entry is complete.
        error: function (xhr, status, error) {
            // do something with the error
        }
        success: function (res) {
            var r = parseInt(res);
            if (r === 1) { //IF Response ok? we've finished the task                
                load_sec(link, 106); //This reloads the section via AJAX
                $('#loaderX').css('display', 'none'); //Here stops the animation
            } else if (r === 0) {
                setTimeout(checkStatus(idArchive), 1000);
            }
        }
    });
}
Charles380
  • 1,269
  • 8
  • 19
  • This is exactly what I would do. If you really wanted a delay between ajax calls: `if (r == 0) { setTimeout(function() { checkStatus(idArchive); }, 1000); }` . Also, you're probably don't want to be using `eval`: http://stackoverflow.com/questions/86513/why-is-using-the-javascript-eval-function-a-bad-idea – Walter Stabosz Feb 20 '14 at 13:22
  • heh I was doing that just as you made that comment – Charles380 Feb 20 '14 at 13:23
  • My point about eval was actually directed at the OP since it was in his code. I wouldn't even `parseInt`, I'd just do a string comparison `if (r === '1')`. – Walter Stabosz Feb 20 '14 at 16:04