使用 OpenLaszlo / javascript 从秒有效计算 MM:ss 格式显示的分钟和秒?
我目前正在使用以下函数,该函数基于网络上的示例,每秒调用一次以通过视频流显示当前进度。
我可以做些什么来提高效率吗?
function secondstominutes(secs){
var s;
if(secs > 60){
var min = Math.floor(secs / 60);
s = min < 10 ? "0" : "";
s += min +":";
secs = secs - min * 60;
} else {
s = "00:";
}
if(secs < 10){
s+= "0" + Math.floor(secs);
} else {
s += Math.floor(secs);
}
return s;
}
I'm currently using the following function which is based on an example from the web, it is called every second to display the current progress through a video stream.
Is there something I could do to make this more efficient?
function secondstominutes(secs){
var s;
if(secs > 60){
var min = Math.floor(secs / 60);
s = min < 10 ? "0" : "";
s += min +":";
secs = secs - min * 60;
} else {
s = "00:";
}
if(secs < 10){
s+= "0" + Math.floor(secs);
} else {
s += Math.floor(secs);
}
return s;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,它可以更简单一点,并且使用更少的 Math.floor、局部变量等。这里我的建议:
这将给出如下结果:
0:1 持续 1 秒
0:10 持续 10 秒
1:1 61 秒
等。
如果你想要空格等,可以这样做:
这个是用于强迫性单行函数 ppls ;)
Yes it can be a little simpler, and uses less Math.floor, local variables etc. Here mine proposition:
This will give result like:
0:1 for 1 sec
0:10 for 10 sec
1:1 for 61 sec
etc.
If you want spaces etc, it could be done like this:
And this one is for obbsesive One-line function ppls ;)
bobwienholt 的版本紧凑、快速且清晰,与我实现它的方式非常相似。 为了好玩,我对一些不同的解决方案进行了一些分析,我发现这要快一些,但是它并不那么清晰,并且在语句中进行半隐藏的赋值:
解释:
将秒除以 60 并二进制“OR”结果与 0。二进制“OR”将值转换为整数,并且通过与 0 进行“或”运算,结果的整数部分将不加修改地返回。 这比调用 Math.floor() 更快,但不太清楚。 由于它被转换为整数,因此您将任何电影的长度限制为 2147483647 分钟 ≈ 35791394 小时 ≈ 1491308 天 ≈ 4085 年。
这是执行(secs = secs % 60)的简写形式。 它将秒的值除以60并指定秒作为提醒(例如61 % 60 = 1)。 通过分析,我发现将该计算放在 return 语句中而不是单独放在一行中要快一些。
这将计算第一个语句 m < 10,如果为 true,则执行第二条语句 "0" + m,否则执行第三条语句:m。 英语:如果 m 小于 10,则在开头添加零,否则按原样返回值。
bobwienholts version is compact, fast and clear and it's very similar to the way I would have implemented it. For fun I did some profiling of a few different solutions and I found this to be a little faster, however it is not as clear and do an assignment semi hidden in a statement:
Explanation:
Divide secs by 60 and binary "OR" the result with 0. The binary "OR" converts the value to an integer and by or-ing with 0 the integer part of the result is returned without modification. This is faster than calling Math.floor() but less clear. Since it is converted to an integer you limit the length of any movie to 2147483647 minutes ≈ 35791394 hours ≈ 1491308 days ≈ 4085 years.
This is the short form of doing (secs = secs % 60). It divides the value of secs by 60 and assigns secs the reminder (example 61 % 60 = 1). By profiling I found that it was a little faster to put that computation inside the return statement instead of on a row of its own.
This computes the first statement m < 10 and if it is true the second statement is executed "0" + m else the third: m. In English: If m is less than 10 then a zero is added at the beginning else the value is returned as is.