Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

29-Countdown Timer 结束时间无法正确显示 #64

Open
GArosy opened this issue Jun 1, 2022 · 0 comments
Open

29-Countdown Timer 结束时间无法正确显示 #64

GArosy opened this issue Jun 1, 2022 · 0 comments

Comments

@GArosy
Copy link

GArosy commented Jun 1, 2022

问题

页面加载一段时间后再点击创建定时器的按钮,显示的结束时间与实际情况不符。

解决

用于获取当前时间的实例 date 在页面加载时就已作为全局变量创建,并且之后无法更新,计时结束的时间均以页面加载的时刻为基准计算,导致出现错误。
可将 Date 实例的创建放在 updateTime 函数内,每次点击按钮就重新获取当前时间:

function updateTime(delta){
    let currentTime = new Date().getTime();
    left = left + parseInt(delta,0);
    end = currentTime + left*1000;
    leftTime.innerHTML = left;
    endTime.innerHTML =new Date(end).toLocaleTimeString();
}

位置

end = date.getTime() + left*1000;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant