【问题标题】:jQuery progress bar with timezone/start date/end date带有时区/开始日期/结束日期的 jQuery 进度条
【发布时间】:2015-07-04 06:22:42
【问题描述】:

我正在使用这个:

var start = new Date();
var maxTime = 835000;
var timeoutVal = Math.floor(maxTime/100);
animateUpdate();

function updateProgress(percentage) {
    $('#pbar_innerdiv').css("width", percentage + "%");
    $('#pbar_innertext').text(percentage + "%");
}

function animateUpdate() {
    var now = new Date();
    var timeDiff = now.getTime() - start.getTime();
    var perc = Math.round((timeDiff/maxTime)*100);
      if (perc <= 100) {
       updateProgress(perc);
       setTimeout(animateUpdate, timeoutVal);
      }
}

https://jsfiddle.net/6h74c/3/

而且它有效。但它仅在您加载页面并开始计算maxTime(以毫秒为单位)时有效。这是没用的。我需要设置开始日期和结束日期,例如3 天 5 小时(包括时区和输入日期时,还需要包括小时)。因此,如果用户在 2 天内访问,他已经看到进度条完成了约 70%,依此类推。

这是一个正在建设的网站的进度条。这样我们就可以让访问者知道该网站需要多长时间才能完成。

【问题讨论】:

    标签: javascript jquery html date progress-bar


    【解决方案1】:

    给你。让我们从 UTC 时区开始。

    // year,month,day,hours,minutes,seconds,millisec
    // set to recent UTC time in past.
    var start = new Date(Date.UTC(2015, 6, 4, 7, 23, 0, 0));
    var maxTime = 835000;
    var timeoutVal = Math.floor(maxTime / 100);
    animateUpdate();
    
    function updateProgress(percentage) {
      $('#pbar_innerdiv').css("width", percentage + "%");
      $('#pbar_innertext').text(percentage + "%");
    }
    
    function animateUpdate() {
      var now = new Date();
      var timeDiff = now.getTime() - start.getTime();
      var perc = Math.round((timeDiff / maxTime) * 100);
      console.log(perc);
      if (perc <= 100) {
        updateProgress(perc);
        setTimeout(animateUpdate, timeoutVal);
      } else { // It got 100%
        updateProgress(100);
      }
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="pbar_outerdiv" style="width: 300px; height: 20px; border: 1px solid grey; z-index: 1; position: relative; border-radius: 5px; -moz-border-radius: 5px;">
      <div id="pbar_innerdiv" style="background-color: lightblue; z-index: 2; height: 100%; width: 0%;"></div>
      <div id="pbar_innertext" style="z-index: 3; position: absolute; top: 0; left: 0; width: 100%; height: 100%; color: black; font-weight: bold; text-align: center;">0%</div>
    </div>

    希望对您有所帮助。

    【讨论】:

    • 您好,请问进度条的开始日期和结束日期应该放在哪里?
    • 我更新了完整的例子。多次刷新页面查看效果。
    • 为什么月份是 6?还有我的意思是,也许我应该把 maxTime 扔掉并用结束日期替换它。因此,如果开始日期 2015-07-03 和结束日期 2015-07-05 那么今天进度条将显示 50%。
    • 记住月份是从零开始的索引(从 0 开始表示一月)。因此,如果您想要 7 月,则月份将为 (7 - 1)。
    • 啊哈,有趣。那么我该如何设置结束日期呢?因为现在我必须设置开始日期并以毫秒为单位计算结束日期?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-19
    • 1970-01-01
    • 1970-01-01
    • 2018-01-31
    • 2015-02-27
    相关资源
    最近更新 更多