使用 Ajax 更新代码隐藏中运行的进程的进度

发布于 2024-11-27 03:24:45 字数 1512 浏览 0 评论 0原文

这是我之前提出的问题的后续。

在进程运行时更新网页

我想要该网页使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲念泪 2024-12-04 03:24:45

setInterval("Check()", 3000); 应在按钮单击事件上调用。不要直接在按钮单击事件上调用 Click(),只需调用 setInterval("Check()", 3000); ,它就会调用 Click () 为您服务。

setInterval("Check()", 3000); should be called on the button click event. Don't invoke Click() directly on the button click event, just invoke the setInterval("Check()", 3000); and it will invoke Click() for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文