【发布时间】:2019-08-21 14:57:03
【问题描述】:
我的代码可以将时间间隔(总和和平均值)转换为“hh:mm:ss”格式,在任何地方都很好,除了在 IE 11 中出现此错误:
SCRIPT438:对象不支持属性或方法“padStart”
如何重写此代码以使其正常工作?
var times = [3600000, 60000, 1000];
function formatTime(avg) {
return times.map(function (t) {
var value = Math.floor(avg / t);
avg %= t;
return value;
}).map(function (v) {
return v.toString().padStart(2, 0);
}).join(':');
}
console.log(formatTime(32939000)); // 09:08:59
padStart(2, 0) 在最后一个 return 语句中被调用。如何让它在 IE11 中运行?
【问题讨论】:
-
你应该找到并使用 polyfill 或使用其他东西 developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
-
只需 polyfill 功能 - 要么找一个 shim 来完成它,要么只是 get the code from MDN
-
谢谢!我添加了这个 polyfill,它起作用了。
标签: javascript arrays internet-explorer-11