如果 DIV 值根据 ActionResult 值发生变化,则停止计时器

发布于 2025-01-19 09:33:51 字数 2263 浏览 0 评论 0原文

我将文件上传到文件夹中,并将其详细信息保存到数据库中。在执行此任务时,我正在启动计时器,插入数据后我有兴趣停止计时器。

<div id="labelUploadingStartTime" name="labelUploadingStartTime"></div>
<div id="labelUploadingEndTime" name="labelUploadingEndTime"></div>

使用以下函数,我在 labelUploadingStartTime 标签上显示开始时间

function startUpdatingUploading_StartTimeIndicator() {       
        intervalIdUploadingStartTime = setInterval(
            function () {                
                $.post(
                    "/MyUpload/GetStartTime",
                    function (progress) {                        
                        $("#labelUploadingStartTime").html(progress);
                    }
                );
            },
            10
        );
    }

同时当在labelUploadingEndTime中设置labelUploadingStartTime值时,我使用以下函数显示倒计时详细信息。

function startUpdatingUploadingEndTimeIndicator() {
       $.post(  
            "/MyUpload/GetStartTime",
            function (progress) {                
                if (progress != null) {
                    intervalIdUploadingEndTime = setInterval(function () {
                        var currentTime = new Date();
                        var hours = currentTime.getHours();
                        var minutes = currentTime.getMinutes();
                        var seconds = currentTime.getSeconds();

                        // Add leading zeros
                        hours = (hours < 10 ? "0" : "") + hours;
                        minutes = (minutes < 10 ? "0" : "") + minutes;
                        seconds = (seconds < 10 ? "0" : "") + seconds;

                        // Compose the string for display
                        var currentTimeString = hours + ":" + minutes + ":" + seconds;
                        $("#labelUploadingEndTime").html(currentTimeString);
                    }, 1000);
                }
            }
        );
    }

现在我的兴趣是,当数据插入数据库时​​,我想停止计时器,并且在 labelUploadingEndTime 中我想显示结束时间。

例如,

labelUploadingStartTime --> 12:10:10

labelUploadingEndTime --> 12:12:30

我怎样才能停止计时器?

I am uploading files into the folder as well as saving their details into DB. While performing this task I am starting my timer And after inserting data I am interested to STOP the timer.

<div id="labelUploadingStartTime" name="labelUploadingStartTime"></div>
<div id="labelUploadingEndTime" name="labelUploadingEndTime"></div>

Using the below function I am displaying the START TIME on my labelUploadingStartTime label.

function startUpdatingUploading_StartTimeIndicator() {       
        intervalIdUploadingStartTime = setInterval(
            function () {                
                $.post(
                    "/MyUpload/GetStartTime",
                    function (progress) {                        
                        $("#labelUploadingStartTime").html(progress);
                    }
                );
            },
            10
        );
    }

At the same time when the labelUploadingStartTime value is set in labelUploadingEndTime I am display the countdown details using below function.

function startUpdatingUploadingEndTimeIndicator() {
       $.post(  
            "/MyUpload/GetStartTime",
            function (progress) {                
                if (progress != null) {
                    intervalIdUploadingEndTime = setInterval(function () {
                        var currentTime = new Date();
                        var hours = currentTime.getHours();
                        var minutes = currentTime.getMinutes();
                        var seconds = currentTime.getSeconds();

                        // Add leading zeros
                        hours = (hours < 10 ? "0" : "") + hours;
                        minutes = (minutes < 10 ? "0" : "") + minutes;
                        seconds = (seconds < 10 ? "0" : "") + seconds;

                        // Compose the string for display
                        var currentTimeString = hours + ":" + minutes + ":" + seconds;
                        $("#labelUploadingEndTime").html(currentTimeString);
                    }, 1000);
                }
            }
        );
    }

Now my interest is when data is inserted into the database I want to STOP THE TIMER and in labelUploadingEndTime I want to display the ending time.

For example,

labelUploadingStartTime -- > 12:10:10

labelUploadingEndTime -- > 12:12:30

How CAN I STOP THE TIMER?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

我一直都在从未离去 2025-01-26 09:33:51

要回答您的问题,您可以使用 clearInterval() JS 函数停止计时器。您传入要停止的计时器 ID,例如:

clearInterval(intervalIdUploadingEndTime);

但是,我认为您根本不需要计时器。您可以使用 $.postsuccess 函数将完成时间添加到其他标签。

function doUpload() {
  var startTime = new Date();
  $("#labelUploadingStartTime").html(startTime.toLocaleString());

  $.post(
    "/MyUpload/PostFiles",
    function () {                        
      var endTime = new Date();
      $("#labelUploadingEndTime").html(endTime.toLocaleString());
    }
  );
}

您的后端不需要返回文件上传的开始和结束时间,您在客户端拥有该信息

To answer your question, you can stop the timer with the clearInterval() JS function. You pass in the timer ID that you want to stop, eg:

clearInterval(intervalIdUploadingEndTime);

However, I don't think you need a timer at all for this. You can use the success function of $.post to add the finish time to the other label.

function doUpload() {
  var startTime = new Date();
  $("#labelUploadingStartTime").html(startTime.toLocaleString());

  $.post(
    "/MyUpload/PostFiles",
    function () {                        
      var endTime = new Date();
      $("#labelUploadingEndTime").html(endTime.toLocaleString());
    }
  );
}

Your backend doesn't need to return the start and end times of the file upload, you have that information on the client side

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