简体   繁体   English

计时器计数太快

[英]Timer is counting too fast

I am creating a browser game and it will have a timer when the game starts.我正在创建一个浏览器游戏,它会在游戏开始时有一个计时器。 I have things working well but I am relatively new to Javascript so tips about the structure of the program are appreciated.我的东西运行良好,但我对 Javascript 相对较新,因此非常感谢有关程序结构的提示。

My problem is that my timer counts milliseconds where it should count secs and secs where it should count minutes.我的问题是我的计时器在应该计算秒的地方计算毫秒,在应该计算分钟的地方计算秒。

timer.js计时器.js

class Timer {           //timer works in s
    
    constructor() {
        this.curr = 0;
        this.initial_time = 0;
        this.t_status = false;
        this.curr_str;          //string for the current time
        this.old_timeStamp = 0;
        this.new_timeStamp = 0;
    }
    
    convert_to_str(s){  //converts time in secs to a mm:ss format
    
        console.log(s);
    
        let minutes = Math.trunc(s / 60);
        let secs = s % 60;
        let min_str;
        let secs_str;
        
        if (minutes < 10) {
            min_str = "0" + Math.trunc(minutes);
        } 
        else {
            min_str = Math.trunc(minutes).toString();
        }
        
        if (secs < 10) {
            secs_str = "0" + Math.trunc(secs);
        }
        else {
            secs_str = Math.trunc(secs).toString();
        }
        
        return min_str + ":" + secs_str;
        
    }
    
    set(s){         //sets time to sex seconds
        this.initial_time = s;
        this.curr = s;
        this.curr_str = this.convert_to_str(this.curr);
    }
    
    decrement(amount) {         //decrements the seconds timer
        this.curr = this.curr - amount;
        this.curr_str = this.convert_to_str(this.curr);
    }
    
    increment() {           //increments the seconds timer
        this.curr = this.curr + 1;
        this.curr_str = convert_to_str(this.curr);
    }
    
    clear() {
        this.curr = 0;
        this.curr_string = "";
        this.initial_time = 0;
    }
    
    get_time() {
        return this.curr_str;
    }   
    
    get_curr() {
        return this.curr;
    }   
}

main.js main.js

function update(timeStamp) {
// Calculate the number of seconds passed since the last frame
timer.new_timeStamp = Date.now();
secondsPassed = (timer.new_timeStamp - timer.old_timeStamp) / 1000;

if (timer.t_status){
    console.log(secondsPassed);
    
    if (timer.get_curr() <= 0) {
        console.log("timer hit 0 stopping");
        timer.t_status = false;
    }
    else if ( secondsPassed >= 1 ){
        oldTimeStamp = Date.now();
        timer.decrement(1);
        
        console.log("changing timer to " + timer.get_time());
        
        $("#timer").html(timer.get_time());     //display current time string
    }
    
    console.log(timer.get_curr());
}

// Calculate fps
fps = Math.round(1 / secondsPassed);

// The loop function has reached it's end. Keep requesting new frames
window.requestAnimationFrame(update);
}

I made various changes to integer literals throughout the update function but no matter what numbers I change the counter counts the same?在整个更新function 中,我对 integer 文字进行了各种更改,但无论我更改什么数字,计数器的计数都相同吗? For example at first I had if (secondsPassed >= 1) but the program had the same behavior.例如,起初我有if (secondsPassed >= 1)但程序具有相同的行为。

Please help:)请帮忙:)

In the function convert_to_str , can you just change let minutes and let secs assignment to the following (note, milliseconds being the param u pass into convert_to_str function): let minutes = milliseconds / 60000;在 function convert_to_str中,您可以更改let minuteslet secs赋值为以下(注意,毫秒是您传递给convert_to_str函数的参数): let minutes = milliseconds / 60000; let secs = milliseconds / 1000 - 60 * minutes;让秒 = 毫秒 / 1000 - 60 * 分钟;

There are a few things wrong with the code.代码有一些问题。 For one, the code is trying to use the timestamp parameter without understanding what it is.一方面,代码试图在不了解它是什么的情况下使用时间戳参数。 It is not not the same as a Date.now() object.它与 Date.now() object 不同。

The easiest solution to this problem was to make my own timestamps in my timer class.这个问题最简单的解决方案是在我的计时器 class 中制作我自己的时间戳。 After that.在那之后。 My code worked perfectly.我的代码运行良好。

Timer.js计时器.js

class Timer {           //timer works in ms
    
    constructor() {
        this.curr = 0;
        this.initial_time = 0;
        this.t_status = false;
        this.curr_str;          //string for the current time
        this.old_timeStamp = 0;
        this.new_timeStamp = 0;
    }
    
    convert_to_str(s){  //converts time in secs to a mm:ss format
    
        console.log(s);
    
        let minutes = Math.trunc(s / 60);
        let secs = s % 60;
        let min_str;
        let secs_str;
        
        if (minutes < 10) {
            min_str = "0" + Math.trunc(minutes);
        } 
        else {
            min_str = Math.trunc(minutes).toString();
        }
        
        if (secs < 10) {
            secs_str = "0" + Math.trunc(secs);
        }
        else {
            secs_str = Math.trunc(secs).toString();
        }
        
        return min_str + ":" + secs_str;
        
    }
    
    set(s){         //sets time to sex seconds
        this.initial_time = s;
        this.curr = s;
        this.curr_str = this.convert_to_str(this.curr);
    }
    
    decrement(amount) {         //decrements the seconds timer
        this.curr = this.curr - amount;
        this.curr_str = this.convert_to_str(this.curr);
    }
    
    increment() {           //increments the seconds timer
        this.curr = this.curr + 1;
        this.curr_str = convert_to_str(this.curr);
    }
    
    clear() {
        this.curr = 0;
        this.curr_string = "";
        this.initial_time = 0;
    }
    
    get_time() {
        return this.curr_str;
    }   
    
    get_curr() {
        return this.curr;
    }   
}

main.js main.js

function update(timeStamp) {

// Calculate the number of seconds passed since the last frame
timer.new_timeStamp = Date.now();
secondsPassed = (timer.new_timeStamp - timer.old_timeStamp) / 1000;

if (timer.t_status){
    console.log(secondsPassed);
    
    if (timer.get_curr() <= 0) {
        console.log("timer hit 0 stopping");
        timer.t_status = false;
    }
    else if ( secondsPassed >= 1 ){
        timer.old_timeStamp = Date.now();
        timer.decrement(1);
        
        console.log("changing timer to " + timer.get_time());
        
        $("#timer").html(timer.get_time());     //display current time string
    }
    
    console.log(timer.get_curr());
}

// Calculate fps
fps = Math.round(1 / secondsPassed);

// The loop function has reached it's end. Keep requesting new frames
window.requestAnimationFrame(update);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM