AddEventListener的工作不正确

发布于 2025-01-21 03:54:11 字数 1047 浏览 4 评论 0原文

AddSessionTimeBTN应该增加屏幕上显示的时间值。当我在没有参数作为事件处理程序的情况下使用函数时,它的功能很好,但是当使用变更时函数的值增加1时,然后事件侦听器开始忽略单击。我在做什么错?

addSessionTimeBtn.addEventListener('click', function () {
    changeTime(sessionMinutes, sessionSeconds, '+', 'Session')
})

reduceSessionTimeBtn.addEventListener('click', function () {
    changeTime(sessionMinutes, sessionSeconds, '-', 'Session')
})

function changeTime(minutes, seconds, operation, period) {
    if (period === 'Session') {
        timerName.innerHTML = 'Session time';
    } else {
        timerName.innerHTML = 'Break time';
    }
    if (operation === '+') {
        seconds += 1;
        if (seconds > 59) {
            seconds = 0;
            minutes += 1;
        }
    } else {
        seconds -= 1;
        if (minutes > 0 && seconds < 0) {
            minutes -= 1;
            seconds = 59;
        } else if (minutes === 0 && seconds < 1) {
            seconds = 0;
        }
    }
    timer.innerHTML = `${timeLength(minutes)}:${timeLength(seconds)}`
}

addSessionTimeBtn is supposed to increase the time value that is displayed on the screen. It works great when i use function without arguments as an event handler but when changeTime function is used value increases by 1 and then event listener starts to ignore clicking. What i'm doing wrong?

addSessionTimeBtn.addEventListener('click', function () {
    changeTime(sessionMinutes, sessionSeconds, '+', 'Session')
})

reduceSessionTimeBtn.addEventListener('click', function () {
    changeTime(sessionMinutes, sessionSeconds, '-', 'Session')
})

function changeTime(minutes, seconds, operation, period) {
    if (period === 'Session') {
        timerName.innerHTML = 'Session time';
    } else {
        timerName.innerHTML = 'Break time';
    }
    if (operation === '+') {
        seconds += 1;
        if (seconds > 59) {
            seconds = 0;
            minutes += 1;
        }
    } else {
        seconds -= 1;
        if (minutes > 0 && seconds < 0) {
            minutes -= 1;
            seconds = 59;
        } else if (minutes === 0 && seconds < 1) {
            seconds = 0;
        }
    }
    timer.innerHTML = `${timeLength(minutes)}:${timeLength(seconds)}`
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

划一舟意中人 2025-01-28 03:54:11

您将整数作为参数传递给函数,因此由于它们是原始值,因此它们是按值传递的,并且您似乎目标的原始变量没有发生变化(在js中不能有指向整数的指针),并且您的计时器始终重置为零。

您可以将 sessionMinutessessionSeconds 存储在一个对象中并传递该对象,该对象作为引用传递,它将发生变化。

const timer = document.getElementById("timer");
const timerName = document.getElementById("timername");

const session = {
  minutes: 0,
  seconds: 0
}

function changeTime(session, operation, period) {
  if (period === 'Session') {
    timerName.innerHTML = 'Session time';
  } else {
    timerName.innerHTML = 'Break time';
  }
  if (operation === '+') {
    session.seconds += 1;
    if (session.seconds > 59) {
      session.seconds = 0;
      session.minutes += 1;
    }
  } else {
    session.seconds -= 1;
    if (session.minutes > 0 && session.seconds < 0) {
      session.minutes -= 1;
      session.seconds = 59;
    } else if (session.minutes === 0 && session.seconds < 1) {
      session.seconds = 0;
    }
  }
  timer.innerHTML = `${session.minutes}:${session.seconds}`; // I don't know what timeLength does so I removed it just to illustrate, it doesn't change the point.
}

document.getElementById("plus").addEventListener('click', function() {
  changeTime(session, '+', 'Session')
});

document.getElementById("minus").addEventListener('click', function() {
  changeTime(session, '-', 'Session')
});
<button id="plus">
+
</button>

<button id="minus">
-
</button>

<p id="timername">
Session
</p>
<p id="timer">
0:0
</p>

You are passing integers as arguments to your function, so as they are primitive values, they are passed by value, and the original variable you seem to target is not mutated (you can't have pointers to integer in js), and your timer is always reset to zero.

You could store your sessionMinutes and sessionSeconds in an object and pass that object, the object being passed as a reference, it will be mutated.

const timer = document.getElementById("timer");
const timerName = document.getElementById("timername");

const session = {
  minutes: 0,
  seconds: 0
}

function changeTime(session, operation, period) {
  if (period === 'Session') {
    timerName.innerHTML = 'Session time';
  } else {
    timerName.innerHTML = 'Break time';
  }
  if (operation === '+') {
    session.seconds += 1;
    if (session.seconds > 59) {
      session.seconds = 0;
      session.minutes += 1;
    }
  } else {
    session.seconds -= 1;
    if (session.minutes > 0 && session.seconds < 0) {
      session.minutes -= 1;
      session.seconds = 59;
    } else if (session.minutes === 0 && session.seconds < 1) {
      session.seconds = 0;
    }
  }
  timer.innerHTML = `${session.minutes}:${session.seconds}`; // I don't know what timeLength does so I removed it just to illustrate, it doesn't change the point.
}

document.getElementById("plus").addEventListener('click', function() {
  changeTime(session, '+', 'Session')
});

document.getElementById("minus").addEventListener('click', function() {
  changeTime(session, '-', 'Session')
});
<button id="plus">
+
</button>

<button id="minus">
-
</button>

<p id="timername">
Session
</p>
<p id="timer">
0:0
</p>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文