这是我的代码:
$(document).ready(function(){
$("#pbo").hide();
$('.pie_progress').asPieProgress({
namespace: 'pie_progress',
size: 100,
barsize: '4'
});
$("#getit").click(function(){
$.post("process.php",
{
yurl: $("#yurl").val()
},
function(data,status){
if(data > 0 ) {
$('#yurl').hide();
$('#getit').hide();
$("#pbo").show();
$('h1').hide();
$('.pie_progress').asPieProgress('start');
$('.pie_progress').asPieProgress('go',data);
} else {
$('#yurl').show();
$('#getit').show();
$("#pbo").hide();
}
});
});
});
当我点击#getit
按钮时,它会执行process.php
并获取返回的数据,该数据又传递给$('.pie_progress').asPieProgress('go',data);
。我的进度条会根据该数据进行更新,但这似乎只发生一次。有没有办法继续发出 POST
请求,直到数据值为 100?
注意:在 process.php 中,我有一个 php exec,它每秒都会回显进度。
最佳答案
将 ajax 调用放在一个函数中(例如:processTo100)并每次调用它怎么样?
类似这样的事情:
$(document).ready(function(){
$("#pbo").hide();
$('.pie_progress').asPieProgress({
namespace: 'pie_progress',
size: 100,
barsize: '4'
});
$("#getit").click(function(){
processTo100();
});
function processTo100() {
$.post("process.php",
{
yurl: $("#yurl").val()
},
function(data,status) {
if(data > 0 ) {
$('#yurl').hide();
$('#getit').hide();
$("#pbo").show();
$('h1').hide();
$('.pie_progress').asPieProgress('start');
$('.pie_progress').asPieProgress('go',data);
if( parseInt(data) <= 100) {
processTo100();
}
} else {
$('#yurl').show();
$('#getit').show();
$("#pbo").hide();
}
});
}
});
关于php - Ajax 循环每秒从 $.post 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32483725/