Php jqueryajax函数问题
我正在尝试运行一个函数,该函数在加载PHP脚本时执行微调器,并刷新一个PHP文件,该文件统计插入的行数以显示脚本的进度 这就是我到目前为止所做的:Php jqueryajax函数问题,php,jquery,Php,Jquery,我正在尝试运行一个函数,该函数在加载PHP脚本时执行微调器,并刷新一个PHP文件,该文件统计插入的行数以显示脚本的进度 这就是我到目前为止所做的: <script type="text/javascript" language="javascript"> // start spinner on button click $(document).ajaxSend(function(spinner) { $("#spinner").show(); }); //
<script type="text/javascript" language="javascript">
// start spinner on button click
$(document).ajaxSend(function(spinner) {
$("#spinner").show();
});
// refresh progress script and output to #content div
function updateProgress(){
$('#content').load('progress.php');
}
myTimer = setInterval( "updateProgress()", 2000 );
// Execute the primary function
$(document).ready(function() {
$("#driver").click(function(event){
$('#stage').load('execute.php');
});
});
// hide spinner and content div when finished
$(document).ajaxStop(function(spinner) {
clearInterval(myTimer);
$("#spinner").fadeOut("fast");
$("#content").fadeOut("fast");
});
</script>
//单击按钮启动微调器
$(文档).ajaxSend(函数(微调器){
$(“#微调器”).show();
});
//刷新进度脚本并输出到#content div
函数updateProgress(){
$('#content').load('progress.php');
}
myTimer=setInterval(“updateProgress()”,2000);
//执行主函数
$(文档).ready(函数(){
$(“#驱动程序”)。单击(函数(事件){
$('#stage').load('execute.php');
});
});
//完成后隐藏微调器和内容div
$(文档).ajaxStop(函数(微调器){
清除间隔(myTimer);
$(“#微调器”)。淡出(“快速”);
$(“内容”)。淡出(“快速”);
});
现在,updateProgress()函数在第一个间隔结束后启动,即使按钮没有按下,所以我假设我必须将它与微调器函数绑定,但我不完全确定如何使其工作
编辑:以下是显示按钮和div的HTML:
<div id="stage">
Click to Import New Data into AssetData Table
<p>
<div id="spinner"><img src="/images/spinner.gif" alt="Loading..."></div>
<div id="content"></div>
<p>
<input type="button" id="driver" value="Load Data" onClick="this.disabled=true;"></div>
单击此处可将新数据导入AssetData表
您需要:
execute.php
应该上传progress.php
发出ajax请求execute.php
时,微调器消失,进度结果消失var myTimer;
$(document).ready(function () {
// Execute the primary function
$("#driver").click(function (event) {
$.ajax({
url: 'http://api.openweathermap.org/data/2.5/forecast?lat=35&lon=139',
success: function (data) {
//$("#someField").html(data); // you put result of executting `execute.php` into #someField field by uncommenting this string
$("#spinner").toggle();
$("#content").fadeOut("fast");
clearInterval(myTimer);
},
error: function (bob) {
// show error
console.log('get error');
clearInterval(myTimer);
},
beforeSend: function () {
myTimer = setInterval(function () {
/* // uncomment this when you will use it with real files and server
$.ajax({
url: 'progress.php',
success: function (data) {
$("#content").html(data);
}
});
*/
$("#content").append("progress data<br>");
console.log('progress executed');
}, 1); // change delay, when you work with real files and server
$("#spinner").toggle();
console.log('ajaxSend handler executed');
}
});
console.log('main function executed');
});
});
var-myTimer;
$(文档).ready(函数(){
//执行主函数
$(“#驱动程序”)。单击(函数(事件){
$.ajax({
网址:'http://api.openweathermap.org/data/2.5/forecast?lat=35&lon=139',
成功:功能(数据){
//$(“#someField”).html(data);//通过取消对该字符串的注释,将执行`execute.php`的结果放入#someField字段
$(“#微调器”).toggle();
$(“内容”)。淡出(“快速”);
清除间隔(myTimer);
},
错误:函数(bob){
//显示错误
log('get error');
清除间隔(myTimer);
},
beforeSend:函数(){
myTimer=setInterval(函数(){
/*//当您将其用于实际文件和服务器时,请取消对此的注释
$.ajax({
url:'progress.php',
成功:功能(数据){
$(“#content”).html(数据);
}
});
*/
$(“#内容”)。追加(“进度数据
”;
console.log(“执行的进度”);
},1);//处理实际文件和服务器时更改延迟
$(“#微调器”).toggle();
log('ajaxSend handler executed');
}
});
log(“执行的主要功能”);
});
});
请看示例(上面的代码示例)
现在,这段代码可以满足您的所有需要。对吧?
不要忘记取消注释某些行(ajax请求)、更改间隔、删除调试输出(例如第29行)等等
注意(并在使用我的代码时更改)execute.php的url
字段。我使用了天气api(例如,您必须将其更改为progress.php
,因为下载这些数据需要一些时间,所以您可以看到结果。删除天气url并将url设置为progress.php
)
另外,您可以查看示例。代码已整理,此版本允许加载文件,然后再加载另一个。然后再加载另一个。+现在myTimer
+setInterval
+我想,功能进度
协同效果更好
希望,这将对您有所帮助。谢谢您的回复。微调器不是问题,它工作正常。我的问题是updateProgress函数在延迟结束后甚至在按下按钮之前执行,并在第一次setInterval时间结束后立即从progress.php查询输出结果。但是,如果我按下按钮微调器立即运行,计算并输出行数,然后在PHP文件(execute.PHP)打开时完成完成了。不用说,我只是想弄清楚如何让函数等到按下按钮后再执行。这是我目前最大的难题。@user3566452我完全更新了我的答案。我想现在我回答了你的问题。感谢你的帮助。不幸的是,execute.php运行时,progress.php没有显示出来。下面是HTML中的其余代码:“code”单击以将新数据导入AssetData表。让我再试一次。不幸的是,execute.php运行时没有显示progress.php。我只是将剩余的HTML与原始代码一起添加,以便更容易查看在,所以希望这有帮助。嗯…Whaaat???只要解释一下你想要实现什么lol。示例:1.按下按钮并发送ajax请求2.按下按钮启动微调器3.在执行脚本和接收数据时停止微调器。(顺便说一句,这是我上面的脚本所做的)