我正在尝试使用jquery knob和php创建一个进度圈。 Jquery旋钮应该通过来自php的变量的ajax / json来获取值,这是迭代的计数 有了这个PHP脚本,我得到了已经完成的总迭代的百分比。 我的想法是每隔20毫秒使用jquery,这里的数字是json_encode($ percentage)。因此它看起来像一个进度循环,并根据从json_encode获得的数字填充颜色($百分比) 请帮帮我,我是jquery的菜鸟,但我知道一点php。
PHP
$urls1 is an array;
$counter = 0;
$total = count($urls1);
foreach ($urls1 as $urls) {
$counter++;
$percentage = ($counter/$total) * 100;
echo json_encode($percentage);
// some morethings to do
}
现在Jquery
var jQuery_1_11_0 = $.noConflict(true);
$(function () {
jQuery_1_11_0('#check').on('submit', function (e) {
var validatef = $("#url").val();
var validaterror = $('#errorvalidate');
if (validatef == 'Enter Domains Separated By New Line -MAX 100 DOMAINS-') {
validaterror.text('Please enter domain names in the text area');
e.preventDefault();
} else {
validaterror.text('');
$("#progressbar").knob({
'draw': function () {
/*$.ajax({
url: 'multipr-process.html', // it's acually php, but i am using htaccess code, it works fine tested on other scripts.
type: 'GET',
data: data,
dataType: 'json',
success: function (result) {
//console.log(result['percentage']);
var currentpercentage = result['percentage'];
I want the result to get it every 20 ms and I believe that I need to place it in a variable, so
I can add it beyond....
}
});*/
//// if I enable this, ajax script above brokes and can't check in the console because it's reloading page.
$(this.i).val(currentpercentage + '%');
}
});
$.ajax({
type: 'post',
url: 'lib/multipr-process.html',
data: $('#check').serialize(),
success: function (data) {
$("#result").html(data); // apple
$("#progressbar").knob().hide();
}
});
e.preventDefault();
} // ending the else
});
});
答案 0 :(得分:1)
不确定你的目的是什么,或者你的$ urls1是如何组成的,但这种可能性至少可以想到,至少是灵感:
PHP简单地返回:
echo json_encode($urls1);
AJAX成功函数:
success: function (data) {
$("#progressbar").knob().hide();
var total = data.length;
var counter = 0;
var dt = setInterval(function() {
if ( !data[counter] ) { clearInterval(dt); }
else { $("#result").html((counter/total) * 100); }
//else { console.log(data[counter]); }
counter++;
}, 2000); // slowed down, if works set to 20
}