我们怎样才能让分钟值在秒达到60后从零开始?

时间:2017-02-20 14:22:30

标签: javascript time

我正在制作项目,需要以" hh:mm:ss"的格式显示一些延迟时间。当秒值达到60(表示分钟值为1)时,秒值再次从零开始。但是,当分钟值达到60(表示小时值为1)时,分钟显示连续值(61,62,63 .....)。当小时值大于零时,我需要显示分钟值从零开始。我该如何实现这一目标?请帮助我,并提前感谢。在我写的代码下面。



let foo:{[k:string]:any}={}




5 个答案:

答案 0 :(得分:2)

你试过模数吗?像

  • 59 % 60 - > 59
  • 60 % 60 - > 0
  • 61 % 60 - > 1
  • 62 % 60 - > 2
  • ...

只需添加minutes = minutes % 60

答案 1 :(得分:2)

只需将分钟上的计算结果更改为(timer / 60) % 60



var timer = 3600 - 5;
setInterval(function () {
  var hours = parseInt(timer / 3600, 10);
  var minutes = parseInt((timer / 60) % 60, 10);
  var seconds = parseInt(timer % 60, 10);
  hours = hours < 10 ? "0" + hours : hours;
  minutes = minutes < 10 ? "0" + minutes : minutes;
  seconds = seconds < 10 ? "0" + seconds : seconds;
  //$scope.delay = $scope.delayTime.label + ": " + hours + ':' + minutes + ':' + seconds;
  console.log('hours ' + hours + ' minutes ' + minutes + ' seconds ' + seconds);
  if (++timer < 0) {
    //$scope.delayTime = false;
  }
}, 1000);
&#13;
&#13;
&#13;

答案 2 :(得分:1)

减去小时内已包含的分钟数,以便正确计算分钟数:

var hours = parseInt(timer / 3600, 10);
var minutes = parseInt(timer / 60 - hours * 60, 10);
var seconds = parseInt(timer % 60, 10);

答案 3 :(得分:1)

实际上,您的minutes变量包含您的整个时间(以小时为单位):

var minutes = parseInt((timer / 60) % 60, 10);

答案 4 :(得分:0)

这是我想要添加到rasmeister的答案:

由于许多浏览器(尤其是移动浏览器)在浏览器标签处于非活动状态时暂停javascript,我建议您使用var start = +new Date(); // casting Date to number results in timestamp setInterval(function () { // get elapsed time in seconds var timer = Math.round((+new Date() - start) / 1000); var hours = parseInt(timer / 3600, 10); var minutes = parseInt((timer / 60) % 60, 10); var seconds = parseInt(timer % 60, 10); hours = hours < 10 ? "0" + hours : hours; minutes = minutes < 10 ? "0" + minutes : minutes; seconds = seconds < 10 ? "0" + seconds : seconds; //$scope.delay = $scope.delayTime.label + ": " + hours + ':' + minutes + ':' + seconds; console.log('hours ' + hours + ' minutes ' + minutes + ' seconds ' + seconds); if (timer < 0) { //$scope.delayTime = false; } }, 1000);个对象:

public partial class CMSModuleLoader
{
    #region "Macro methods loader attribute"

    /// <summary>
    /// Module registration
    /// </summary>
    private class CustomGatewayLoaderAttribute : CMSLoaderAttribute
    {
        /// <summary>
        /// Constructor
        /// </summary>
        public CustomGatewayLoaderAttribute()
        {
            // Require E-commerce module to load properly
            RequiredModules = new string[] { ModuleName.ECOMMERCE };
        }


        /// <summary>
        /// Initializes the module
        /// </summary>
        public override void Init()
        {
            // This line provides the ability to register the classes via web.config cms.extensibility section from App_Code
            ClassHelper.OnGetCustomClass += GetCustomClass;
        }


        /// <summary>
        /// Gets the custom class object based on the given class name. This handler is called when the assembly name is App_Code.
        /// </summary>
        private static void GetCustomClass(object sender, ClassEventArgs e)
        {
            if (e.Object == null)
            {
                // Provide your custom classes
                switch (e.ClassName.ToLower())
                {
                    // Define the class CustomGatewayProvider inheriting the CMSPaymentGatewayProvider and you can customize the provider
                    case "customgatewayprovider":
                        e.Object = new CustomGatewayProvider();
                        break;
                }
            }
        }
    }

    #endregion
}