Uncaught ReferenceError,它说startTimer未定义,尽管它是定义好的。

huangapple go评论68阅读模式
英文:

Uncaught ReferenceError, it says startTimer is not defined even though it is

问题

const startBtn = document.querySelector('.startBtn');
const pauseBtn = document.querySelector('.pauseBtn');
const ResetBtn = document.querySelector('.resetBtn');
const time = document.querySelector('.time');

let seconds = 0;
let secondsStr = '00';
let minutes = 0;
let minutesStr = '00';
let hours = 0;
let hoursStr = '00';
let timeText = `${hoursStr}:${minutesStr}:${secondsStr}`;
time.textContent = timeText;

function addSecond() {
    seconds++;
    secondsStr = seconds.toString();
    if (seconds < 10) {
        secondsStr = `0${secondsStr}`;
    }

    if (seconds >= 60) {
        seconds = 0;
        minutes++;
        minutesStr = minutes.toString();
        if (minutes < 10) {
            minutesStr = `0${minutesStr}`;
        }

        if (minutes >= 60) {
            minutes = 0;
            hours++;
            hoursStr = hours.toString();
            if (hours < 10) {
                hoursStr = `0${hoursStr}`;
            }
        }
    }
    timeText = `${hoursStr}:${minutesStr}:${secondsStr}`;
    time.textContent = timeText;
}

let startTimer; // Declare the startTimer variable outside the functions.

startBtn.addEventListener('click', startWatch);
pauseBtn.addEventListener('click', pauseWatch);

function startWatch() {
    startTimer = setInterval(addSecond, 1000); // Assign the interval to the startTimer variable.
}

function pauseWatch() {
    clearInterval(startTimer); // Use the startTimer variable to clear the interval.
}

In the code provided, I declared the startTimer variable outside of the functions to make it accessible to both the startWatch and pauseWatch functions. This way, the clearInterval function can properly clear the interval when pauseWatch is called.

英文:
const startBtn = document.querySelector(&#39;.startBtn&#39;);
const pauseBtn = document.querySelector(&#39;.pauseBtn&#39;);
const ResetBtn = document.querySelector(&#39;.resetBtn&#39;);
const time = document.querySelector(&#39;.time&#39;);
let seconds = 0;
let secondsStr = &#39;00&#39;;
let minutes = 0;
let minutesStr = &#39;00&#39;;
let hours = 0;
let hoursStr = &#39;00&#39;;
let timeText = `${hoursStr}:${minutesStr}:${secondsStr}`;
time.textContent = timeText;
function addSecond() {
seconds++;
secondsStr = seconds.toString();
if (seconds &lt; 10) {
secondsStr = `0${secondsStr}`;
}
if(seconds &gt;= 60) {
seconds = 0;
minutes++;
minutesStr = minutes.toString();
if (minutes &lt; 10) {
minutesStr = `0${minutesStr}`;
}
if(minutes &gt;= 60) {
minutes = 0;
hours++;
hoursStr = hours.toString();
if (hours &lt; 10) {
hoursStr = `0${hoursStr}`;
}
}
}
timeText = `${hoursStr}:${minutesStr}:${secondsStr}`;
time.textContent = timeText;
}
startBtn.addEventListener(&#39;click&#39;, startWatch);
pauseBtn.addEventListener(&#39;click&#39;, pauseWatch);
function startWatch() {
const startTimer = setInterval(addSecond, 1000);
}
function pauseWatch() {
clearInterval(startTimer);
}
&lt;div class=&quot;stopwatch&quot;&gt;
&lt;div class=&quot;time&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;stopwatchBtns&quot;&gt;
&lt;button class=&quot;startBtn stopwatchBtn&quot;&gt;Start&lt;/button&gt;
&lt;button class=&quot;pauseBtn stopwatchBtn&quot;&gt;Pause&lt;/button&gt;
&lt;button class=&quot;resetBtn stopwatchBtn&quot;&gt;Reset&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;

basically when the pauseWatch function is called i want it to clear the startTimer interval but it says that it is not defined even though it is im not sure if its to do with the variable scope but even if it is i dont know how to fix it

i tried rearranging some code but nothing seem to help at all

答案1

得分: 1

你需要在全局范围内(函数外部)定义startTimer变量。这样可以使它在你的startWatch()函数外部使用。

const startBtn = document.querySelector('.startBtn');
const pauseBtn = document.querySelector('.pauseBtn');
const ResetBtn = document.querySelector('.resetBtn');
const time = document.querySelector('.time');

let seconds = 0;
let secondsStr = '00';
let minutes = 0;
let minutesStr = '00';
let hours = 0;
let hoursStr = '00';
let timeText = `${hoursStr}:${minutesStr}:${secondsStr}`;
time.textContent = timeText;

let startTimer; // 在全局范围内定义变量

function addSecond() {

...
英文:

You need to define the startTimer variable in the global scope (outside of the function). This will allow it to be used outside of your startWatch() function.

const startBtn = document.querySelector(&#39;.startBtn&#39;);
const pauseBtn = document.querySelector(&#39;.pauseBtn&#39;);
const ResetBtn = document.querySelector(&#39;.resetBtn&#39;);
const time = document.querySelector(&#39;.time&#39;);

let seconds = 0;
let secondsStr = &#39;00&#39;;
let minutes = 0;
let minutesStr = &#39;00&#39;;
let hours = 0;
let hoursStr = &#39;00&#39;;
let timeText = `${hoursStr}:${minutesStr}:${secondsStr}`;
time.textContent = timeText;

let startTimer; // define variable in global scope

function addSecond() {

...

答案2

得分: 0

StartTimer位于一个函数内,无法调用。我在代码开头添加了var startTimer;并稍微编辑了函数startWatch()

var startTimer;

function startWatch() {
    startTimer = setInterval(addSecond, 1000);
}

请尝试访问:
https://replit.com/@JLRR222/problem#index.html

英文:

The StartTimer is inside a function, where it cannot be called.
I added var startTimer; at the beginning of the code and i edited the function startWatch() a bit

var startTimer;
function startWatch() {
startTimer = setInterval(addSecond, 1000);
}

Try
https://replit.com/@JLRR222/problem#index.html

huangapple
  • 本文由 发表于 2023年3月15日 19:52:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75744335.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定