ajax 短轮询_一个简单的ajax轮询

本文展示了如何利用Ajax实现短轮询来实时监控系统升级或数据备份的进度。通过点击开始和停止按钮控制轮询,初始化并获取升级信息,轮询获取更新状态并更新进度条显示。在轮询过程中,还会将更新日志追加到页面上,提供详细的备份和检查操作。
摘要由CSDN通过智能技术生成

window.stepTotal = 0;

window.stepNum = 0;

window.updateRun = false;

$('#update-start').click(function(){

window.updateRun = true;

updateStart();

})

$('#update-stop').click(function(){

window.updateRun = false;

})

$('#update-init').click(function(){

$('#ds-check-task-sys-log').html('');

$('#update-table-box').find('.progress-info').html('');

window.stepNum = 0;

window.stepTotal = false;

setTotalProgress();

initTableProgress();

$.ajax({//获取升级信息 url:'?r=data-update/initialization',

type:'GET',

dataType:'JSON',

async:false,

success:function(data){

if (data.code==0) {

alert(data.text);

}

},

});

})

function updateStart(){

updateInit();

return false;

updateStep();

}

//清除备份function updateClear(){

//检查是否可以执行备份操作 $.ajax({

url:'?r=data-back/update-clear',

type:'POST',

dataType:'JSON',

async:false,

data:{xconfirm:1},

success:function(data){

alert(data.text);

},

error:function(){

}

})

}

function updateCheck(){

var result = {code:'1', text:'备份检查未执行'};

//检查是否可以执行备份操作 $.ajax({

url:'?r=data-back/update-check',

type:'POST',

dataType:'JSON',

async:false,

success:function(data){

result = data;

},

error:function(){

result = {code:'1', text:'备份检查失败'};

}

})

return result;

}

function updateInit(){

$.ajax({//获取升级信息 url:'?r=data-update/get-update-info',

type:'POST',

dataType:'JSON',

async:false,

success:function(data){

window.stepTotal = data.step_total;

window.stepNum = 0;

setTotalProgress();

},

error:function(xhr){

}

});

polling();

}

function polling(){

$.ajax({//获取轮询 url:"/?r=data-update/polling",

type:"GET",

async:true,

dataType:"json",

success:function (msg){

//console.log(msg); if (msg.code==0 || msg.code==2){

if (msg.code==2){

polling_step();

}

window.stepNum = msg.step;

setTotalProgress();

$('#ds-check-task-sys-log').append(msg.text+'
');

polling();

} else {

alert(msg.text);

}

}

});

}

function polling_step(){

$.ajax({//获取轮询 url:"/?r=data-update/polling_step",

type:"GET",

async:true,

dataType:"json",

success:function (msg){

console.log(msg);

if(msg.stop==1 && msg.code!='info') {

var info = {

total:msg.step,

page:msg.code,

pagetotal:msg.text

};

$('#update-table-box').find('.progress-info').html('共有'+info.total+'条记录,备份进度['+info.page+'/'+info.pagetotal+'页]');

setTableProgress(info);

polling_step();

}

if (msg.code=='info'){

var content = $('#ds-check-task-sys-log').text();

var str=new RegExp(msg.text);

if(!str.test(content)){

$('#ds-check-task-sys-log').append(msg.text+'
');

}

polling_step();

}

}

});

}

function setTotalProgress(){

var tableNum = window.tableIndex;

if(window.stepTotal > 0){

var percent = Math.ceil((stepNum/window.stepTotal)*100);

}else{

var percent = 100;

}

var progressBox = $('#update-total-box');

progressBox.find('.step-total').html(window.stepTotal);

progressBox.find('.step-num').html(window.stepNum);

progressBox.find('.progress-bar').attr('aria-valuenow', window.stepNum);

progressBox.find('.progress-bar').attr('aria-valuemax', window.stepTotal);

progressBox.find('.progress-bar').width(percent+'%');

progressBox.find('.progress-bar').html(percent+'%');

}

function updateStep(stepNum, page){

if(!window.updateRun){

return false;

}

if(typeof(page) == 'undefined'){

var page = 1;

$('#update-table-box').find('.progress-info').html('共有0条记录,备份进度[0/0页]');

initTableProgress();//初始化进度条 }

var data = {table:table, pagesize:window.updatePagesize, page:page};

$.ajax({//获取数据库信息 url:'?r=data-update/step'+stepNum,

type:'POST',

dataType:'JSON',

data:data,

success:function(data){

if(data.code == 0){

var info = data.info;

setTableProgress(info);

//备份下一张表格 setTimeout(function(){

window.tableIndex++;

setTotalProgress();

updateTable();

}, 1000)

}else{

//备份下一页表数据 updateStep(stepNum, page + 1);

}

},

error:function(xhr){

}

})

}

function initTableProgress(){

var progressBox = $('#update-table-box');

progressBox.find('.progress-bar').attr('aria-valuenow', 0);

progressBox.find('.progress-bar').attr('aria-valuemax', 0);

progressBox.find('.progress-bar').width('0%');

progressBox.find('.progress-bar').html('0%');

}

function setTableProgress(info){

var progressBox = $('#update-table-box');

progressBox.find('.total-num').html(info.total);

progressBox.find('.current-page-num').html(info.page);

progressBox.find('.pagetotal-num').html(info.pagetotal);

if(info.pagetotal > 0){

var percent = Math.ceil((info.page/info.pagetotal)*100);

}else{

var percent = 100;

info['page'] = 1;

info['pagetotal'] = 1;

}

progressBox.find('.progress-bar').attr('aria-valuenow', info.page);

progressBox.find('.progress-bar').attr('aria-valuemax', info.pagetotal);

progressBox.find('.progress-bar').width(percent+'%');

progressBox.find('.progress-bar').html(percent+'%');

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值