gpt4 book ai didi

使用循环舍入值的Javascript函数..回调?

转载 作者:行者123 更新时间:2023-11-28 08:29:49 31 4
gpt4 key购买 nike

有 2 个 HTML 下拉列表,一个用于 12 小时时间,一个用于每小时 5 分钟的时间间隔。

..
<select class="form-control" name="hour" id="hour">
<option>1</option>
..
<option>12</option>
</select>
..
<select class="form-control" name="minute" id="minute">
<option>0</option>
..
<option>55</option>
</select>
..

一直在尝试使用 if/elseif 来四舍五入值的另一个函数(可以做一些事情)内部编写一个 javascript 函数,但它需要帮助..也许是回调函数或匿名函数?

var hours = date.getHours();
var minutes = date.getMinutes();

function hour(hours)
{
// account for 24-hour clock
if (hours > 12)
{
hours = hours - 12
};

// account for 0 in 24
else if (hours == 0)
{
hours = 12
};
}

// round to the 5minute marks
if (minutes >= 0 && minutes < 3) {minutes = 0};
else if (minutes >= 3 && minutes < 8) {minutes = 5};
..
else if (minutes >= 53 && minutes < 58) {minutes = 55};

// round 58/59/60 to the next hour
else (minutes >= 58 && minutes < 60) {minutes = 0 && hours = hours + 1};

// apply the rounded numbers
$('#hour').val(hours);
$('#minute').val(minutes);

最佳答案

提示:更好的舍入分钟数的方法,而不是大量 if/else if:

var offset=minutes%5;
if(offset < 3) minutes=minutes-offset;
if(offset >=3) minutes = minutes+(5-offset);
if(minutes==60){
minutes=0;hours+=1;
}

关于使用循环舍入值的Javascript函数..回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22004029/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com