使用 Ajax 更新代码隐藏中运行的进程的进度
这是我之前提出的问题的后续。
我想要该网页使用 Ajax 显示进程正在运行的阶段。在后面的代码中的每个步骤之后,文件都会被一些文本覆盖,指示刚刚发生了什么。我希望网页定期发送对该文件的请求并使用 setInterval 或 setTimeout 显示其内容。一旦按下按钮,网页就开始其请求周期,服务器开始其耗时的操作。
我现在拥有的...
定期请求的脚本:
<script type="text/javascript">
var d = setInterval("Check()", 3000);
function Check() {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
alert("get chrome");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4) {
document.getElementById("s").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "temp/test.txt", true);
xmlhttp.send();
}
/*$(document).ready(function () {
$("#uploadbutton").click(Check());
});*/
</script>
当前 Check() 在页面加载后立即开始运行。我希望只有在单击事件发生时才调用它。如果我在单击事件中定义 Check(),它不会识别 setInterval,并且只会检查我是否再次单击该按钮。这不是一个大问题,因为我认为运行脚本就可以了。它只会显示一个“未搜索”字符串。不幸的是,随着文件更新(并且我检查过),页面在获取响应时不会显示内容。它只是保持不变,然后一旦代码隐藏完成,它就会重置为默认设置。谁能告诉我为什么页面不更新?
编辑:忘记提及 click 事件有一个 onclick 分配给执行长函数的 C# 代码隐藏。我不知道是否可以运行 javascript Ajax 函数以及 C# 代码隐藏功能。 Ajax 调用具有 async = true,但脚本部分没有与代码隐藏并行运行,对吧?
再次编辑:我将脚本更改为向上计数。我预计它会在代码隐藏完成其工作时继续计数,但是一旦您按下按钮,它就会停止。一旦代码隐藏完成,它就会重新开始计数。为什么计数不继续进行?我希望它在代码隐藏完成后重新启动,但我认为它会继续计数。
This is a followup on a question I asked earlier.
Update a Web Page as a Process Runs
I want the webpage to display what stage the process is running using Ajax. After each step in the code behind, a file is overwritten with some text indicating what just happened. I want the webpage to periodically send a request for that file and display its contents using either setInterval or setTimeout. Once the button is pressed, the webpage starts its request cycle and the server begins its time consuming operation.
What I have now...
Script for periodic requests:
<script type="text/javascript">
var d = setInterval("Check()", 3000);
function Check() {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
alert("get chrome");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4) {
document.getElementById("s").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "temp/test.txt", true);
xmlhttp.send();
}
/*$(document).ready(function () {
$("#uploadbutton").click(Check());
});*/
</script>
Currently Check() starts running as soon as the page loads. I would like it to only be called once the click event happens. If I define the Check() inside the click event, it doesn't recognize the setInterval and will only check if I click the button again. This isn't a huge problem as I think it will be ok to have the script running. It will just display a "not searching" string. Unfortunately as the file gets updated (and I checked that it was) the page does not display the contents as it gets the response. It just remains the same, then once the codebehind finishes, it resets to the default setting. Can anyone tell me why the page isn't updating?
edit: forgot to mention that the click event has an onclick assigned to the C# codebehind that does the long function. I don't know if I can have the javascript Ajax function running and the C# codebehind going too. The Ajax calls have async = true, but the script part isn't running parallel to codebehind right?
edit again: I changed the script to count upwards. I expected it to continue to count while the codebehind did its thing, but once you hit the button it stops. Once the codebehind finishes it restarts the count. Why doesn't the counting keep going? I expect it to restart once the codebehind is finished, but I thought it would keep counting.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
setInterval("Check()", 3000);
应在按钮单击事件上调用。不要直接在按钮单击事件上调用Click()
,只需调用setInterval("Check()", 3000);
,它就会调用Click ()
为您服务。setInterval("Check()", 3000);
should be called on the button click event. Don't invokeClick()
directly on the button click event, just invoke thesetInterval("Check()", 3000);
and it will invokeClick()
for you.