在setInterval中调用WebService

时间:2014-02-20 12:45:36

标签: javascript jquery html5 web-services

一如既往,请原谅我的'泰山'英语,请:P

问题是: 我必须按时间间隔调用webService,以确保完全上传联系人列表。

程序是:
1.-单击按钮发送CSV文件 2.-在将CSV上传到数据库时,必须看到带有上传动画的div 3.-当websService结束输入数据时,带有卸载动画的div必须消失。

我已经创建了一个“我认为”的JS函数,如图所示:

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);
}

我做错了吗?在setInterval proc中调用WS是否正确?

提前感谢所有人!

2 个答案:

答案 0 :(得分:1)

只需执行以下操作并将时间增加至少5秒。

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);

答案 1 :(得分:1)

根据检查过程,您不需要setInterval,只需再次调用该函数即可。我会直接做直接的ajax电话。请小心,因为它可能导致无限循环

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);
            }
        }
    });
}