我在打字稿上做了一个倒数计时器 现在我们需要这样做,以便在重启后不重置计时器,存储在localStorage中 像所有真正的逻辑制作,但仍然抛出
以下代码:
LS 类
export class LS {
public set (key: string, data: any) {
let result = (data.constructor == Object) ? JSON.stringify(data) : data;
localStorage.setItem(key, result);
}
public get (key: string) : any {
let jsonObject = null;
let data = localStorage.getItem(key);
try {
jsonObject = JSON.parse(data);
}
catch(e) {
jsonObject = data;
}
return jsonObject;
}
public rm(key:string) {
localStorage.removeItem(key);
}
}
倒计时服务类
export class CountdownService {
protected timeData: Object = {
days: 0,
hours: 0,
minutes: 0,
seconds: 0
};
public ONE_SECONDS = 1000;
protected callback:Function;
protected ls:LS;
protected nameTimeData:string = 'timingData';
constructor() {
this.ls = new LS();
}
public start(hours: number, minutes:number, _callback:Function) {
this.callback = _callback;
let milliseconds = this.toMilliseconds(hours, minutes);
let deadline = new Date(Date.parse(new Date().toString()) + milliseconds);
this.initializeClock(deadline);
}
public getTimeData():Object {
return this.timeData
}
protected toMilliseconds(hours, minutes) {
let secondsHours = hours * 3600;
let secondsMinutes = minutes * 60;
return (secondsHours + secondsMinutes) * this.ONE_SECONDS;
}
protected getTimeRemaining(endTime) {
let currentTime = new Date().toString();
/*let lsTime;
// This block does not work correctly
if (this.ls.get(this.nameTimeData) != null) {
lsTime = this.ls.get(this.nameTimeData);
console.log(lsTime);
this.ls.set(this.nameTimeData, new Date().toString());
} else {
this.ls.set(this.nameTimeData, new Date().toString());
lsTime = this.ls.get(this.nameTimeData);
}
*/
let t = Date.parse(endTime) - Date.parse( currentTime );
let seconds = Math.floor((t / this.ONE_SECONDS) % 60);
let minutes = Math.floor((t / this.ONE_SECONDS / 60) % 60);
let hours = Math.floor((t / (this.ONE_SECONDS * 60 * 60)) % 24);
let days = Math.floor(t / (this.ONE_SECONDS * 60 * 60 * 24));
return {
'total': t,
'days': days,
'hours': hours,
'minutes': minutes,
'seconds': seconds
};
}
protected initializeClock(endTime) {
let updateClock = () => {
let t = this.getTimeRemaining(endTime);
this.timeData['days'] = t['days'];
this.timeData['hours'] = ('0' + t.hours).slice(-2);
this.timeData['minutes'] = ('0' + t.minutes).slice(-2);
this.timeData['seconds'] = ('0' + t.seconds).slice(-2);
if (t.total <= 0) {
clearInterval(timeInterval);
this.callback();
}
};
updateClock();
var timeInterval = setInterval(updateClock, this.ONE_SECONDS);
}
}
致电代码
let timeData = this.test['time'].split(':');
console.log(timeData); // ["1", "1"]
this.cds.start(timeData[0], timeData[1], this.endTestCallback);
答案 0 :(得分:1)
看起来调用代码设置相对于当前时间的结束时间。每次刷新页面时,它都会重新计算新的结束时间,计数器似乎会重置。
您需要调整逻辑 - 我建议存储结束时间。这可以在CountdownService
课程之外完成。
调整CountdownService.start()
方法以获取Date
对象(表示绝对时间点,而不是与当前时间的相对偏移量)。
public start(deadline: Date, _callback:Function) {
this.callback = _callback;
this.initializeClock(deadline);
}
在您使用CountdownService
的代码中,如果尚未设置结束日期,请使用LS
服务保存结束日期。像这样:
let storage = new LS();
let deadline = storage.get('deadline');
if (!deadline) { // set end time if not set already
let timeData = this.test['time'].split(':');
console.log(timeData); // ["1", "1"]
// `+new Date` is a trick for converting a date to milliseconds
deadline = new Date(+new Date() + (timeData[0] * 3600 + timeData[1] * 60) * 1000);
}
this.cds.start(deadline, this.endTestCallback);