我正在尝试使用 jquery knob 和 php 制作一个进度圈。 Jquery 旋钮应该通过 ajax/json 从计算迭代次数的 php 获取变量的值 使用此 php 脚本,我得到了已完成的总迭代的百分比。 这个想法是使用 jquery,每 20 毫秒从这里 json_encode($percentage) 获取数字。所以它看起来像一个进度圈,并根据从 json_encode($percentage) 获得的数字用颜色填充它 请帮助我,我是 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
});
});
最佳答案
不确定您的目的是什么或您的 $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
}
关于javascript - 通过 json 以 10ms 的间隔检查/显示变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24014289/