Javascript 如何查看设置计时器倒计时角度7
计时器启动时,如何查看计时器的倒计时?我尝试将其绑定到变量,但结果显示为Javascript 如何查看设置计时器倒计时角度7,javascript,typescript,angular7,Javascript,Typescript,Angular7,计时器启动时,如何查看计时器的倒计时?我尝试将其绑定到变量,但结果显示为[object object]: setTime(时间){ 如果(this.settime>=5){ 这个.currentTime=时间; this.alerttime=this.settime; this.notifiy.showInfo('this.settime+'分钟列车的计时器设置为'+time+'分钟',''); const val=this.settime*60000; 常数源=计时器(val); 这个时间=
[object object]
:
setTime(时间){
如果(this.settime>=5){
这个.currentTime=时间;
this.alerttime=this.settime;
this.notifiy.showInfo('this.settime+'分钟列车的计时器设置为'+time+'分钟','');
const val=this.settime*60000;
常数源=计时器(val);
这个时间=来源;
log(this.time);//这将打印出obserwBale函数,我也尝试将其绑定到subscribe方法中,但没有joy。
const subscribe=source.subscribe(值=>{this.alertTimer();});
this.settime='';
//cordova.plugins.backgroundMode.enable();
}否则{
此.notifiy.showFailure('您不能将计时器设置在5分钟以下','');
}
}
简短的回答是“您不能”,因为您正在使用的实际计时器没有此属性,例如
但是,根据您想用它做什么,您可能希望单独跟踪实际经过的时间,为此您可以使用“interval”
假设您设置了一个计时器()
,持续120秒,因此它会在计划结束时执行。
然后使用一个单独的1s的interval()
来跟踪经过的时间,并更新UI等,直到主计时器结束
伪代码,显示了这个概念。从我的头顶上看,可能有打字错误之类的:
class TimerManager {
private totalSeconds;
private elapsedSeconds;
private timerSub;
private intervalSub
// Start the timer
public startTimer(timerLengthSeconds) {
this.totalSeconds = timerLengthSeconds;
var t = new timer(timerLengthSeconds);
this.timerSub = t.subscribe(t => this.endTimer());
var i = new interval(1000);
this.intervalSub = i.subscribe(i => this.intervalElapsed());
}
// On each interval
private intervalElapsed() {
this.elapsedSeconds++;
var remaining = this.totalSeconds - this.elapsedSeconds;
console.log(this.elapsedSeconds + "s have elapsed, there are " + remaining + "s left");
}
// When the main timer ends
private endTimer() {
console.log("The time is up!!");
this.intervalSub.unsubscribe();
}
}
您可以通过检查设置间隔功能查看计时器倒计时。这里有一个例子。您甚至可以在类型脚本中使用此选项。我想这可以解决你的问题
//120秒计时器。。。
var timeleft=120;
var downloadTimer=setInterval(函数(){
if(timeleft)您能解释一下参数i
应该做什么吗?它不会出现在您的setTime()中
implementation,仅在方法签名中。抱歉,现在纠正了错误。抱歉,我不完全理解,当我尝试时,如何让它从设定的时间倒计时,间隔倒计时,这只是数学,从总数中减去经过的秒数…我添加了一个示例来展示我的想法首先非常感谢对于您的答案,非常感谢,在控制台中,当它打印出经过的时间时,它会显示出来,但它在打印到console@conor我只是把它写在我的头顶上,所以我确信它有错误-在这里它正在工作se行为受可观察到的影响,并使用next(timeleft)在时间变量更新代码的位置对其进行更新。这可能是您的解决方案。