我试图将我在计时器中使用的时间转换为hh:mm:ss格式,使用矩js,但被卡住了。下面是我的密码。
<iron-timer id="timer" start-time="150" end-time="0" current-time="[[currentTime]]">
<moment-js date="[[_calculatedTime(currentTime)]]"></moment-js>
</iron-timer>因此,当时间开始时,时间不会在格式化日期的情况下更新。我怎么才能让它起作用呢?
发布于 2018-03-02 10:09:25
这是我想出的,但你的问题有点不完整,所以如果我走错了路,请纠正我。
组分依赖性:聚合物2.5,铁定时器2.1.2,力矩-js 0.7.2
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Timer</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="import" href="../bower_components/polymer/polymer.html">
<link rel="import" href="../bower_components/iron-timer/iron-timer.html">
<link rel="import" href="../bower_components/moment-js/moment-js.html">
</head>
<body>
<dom-module id="moment-timer">
<template>
<iron-timer id="timer" start-time="150" end-time="0" current-time="{{currentTime}}"></iron-timer>
<moment-js date="[[_toMilliSecs(currentTime)]]" format="HH:mm:ss" utc></moment-js>
</template>
<script>
class MomentTimer extends Polymer.Element{
static get is(){
return 'moment-timer';
}
static get properties(){
return {
currentTime: Number
}
}
_toMilliSecs(currentTime){
return currentTime * 1000;
}
ready(){
super.ready();
this.$.timer.start();
}
}
window.customElements.define(MomentTimer.is,MomentTimer);
</script>
</dom-module>
<moment-timer></moment-timer>
</body>
</html>有几件事需要注意:
super.ready()。currentTime属性作为毫秒传递给moment-js元素--我使用一个方法_toMilliSecs来完成这个任务。utc元素上设置moment-js属性。这是必要的,为您的计数-下降提供一个开始参考。实际上,我们让它计算1970-01-01 :00:00的时间,并设置相对于此的计时器。效果是一样的。希望这能帮上忙!
https://stackoverflow.com/questions/49038379
复制相似问题