我正在尝试创建一个每分钟递增 +1 的计数器。 (一小时的完美值约为 70。)
但是,柜台应从早上 6 点开始工作,一直工作到晚上 8 点(工作时间),周一至周五(工作日)。对于周末和非工作时间,应暂停,但应显示已计算的值。第二天将从最后一个值继续。
计数器将在月底停止,重置,每月 1 日重新启动(如果是工作日)
function startCounter() {
// Get the current date and time
var currentDate = new Date();
var currentDay = currentDate.getDay(); // 0: Sunday, 1: Monday, ..., 6: Saturday
var currentHour = currentDate.getHours();
// Check if it's a weekend or outside working hours
var isWeekend = currentDay === 0 || currentDay === 6; // Sunday or Saturday
var isOutsideWorkingHours = currentHour < 6 || currentHour >= 20; // Before 6am or after 8pm
// If it's a weekend or outside working hours, pause the counter
if (isWeekend || isOutsideWorkingHours) {
console.log('Counter paused.');
return;
}
// Get the starting date for the counter
var startingDate = new Date(currentDate.getFullYear(), currentDate.getMonth(), 1); // 1st of the current month
// Calculate the number of days since the starting date, excluding weekends
var daysSinceStart = 0;
for (var i = 0; i <= currentDay; i++) {
if (i !== 0 && i !== 6) {
daysSinceStart++;
}
}
// Calculate the initial value of the counter based on the number of previous days
var initialValue = daysSinceStart * 1440; // 1440 minutes in a day
// Initialize the counter with the initial value
var counter = initialValue;
// Increment the counter every minute
var intervalId = setInterval(function() {
counter++;
console.log('Counter:', counter);
}, 60000); // 60000 milliseconds = 1 minute
// Display the counter value on page refresh
console.log('Counter:', counter);
}
// Start the counter
startCounter();
我在这里缺少什么?或者整个逻辑必须更新?
谢谢!
最佳答案
美好的一天! 首先: 如果只计算工作时间,如您所示,每天只有 14 小时,而不是 24 小时。这样您每天将增加的不是 1440 分钟,而是 840 分钟。但更好的是创建几个变量(endHour 和 startHour)以确保在任何地方都能正确计数。 如果您想显示计数,即使不是工作时间,但不计数时检查时不返回。
在代码中计算过去的天数不起作用。您算一下,一个月只有一周。
而且你不会计算从一天开始到现在过去了多少分钟。 通过 setInterval 进行计数并不是最好的主意,但还是放在这里进行测试。 所以,这是我的代码,我针对不同的开始时间和结束时间以及 weekEndDays 对其进行了测试:
function startCounter() {
// Get the current date and time
var currentDate = new Date();
var currentDay = currentDate.getDay(); // 0: Sunday, 1: Monday, ..., 6: Saturday
var currentHour = currentDate.getHours();
var currentMinutes = currentDate.getMinutes();
var currentDayOfMonth = currentDate.getDate();
// Check if it's a weekend or outside working hours
var weekEndDays = [0,6]; // weekend days - you can add or delete some days
var isWeekend = weekEndDays.indexOf(currentDay) > -1 ; // Is a weekend?
var startHour = 6;
var endHour = 20;
var isOutsideWorkingHours = currentHour < startHour || currentHour >= endHour; // Before startHour or after endHour
// Get the starting date for the counter
var startingDate = new Date(currentDate.getFullYear(), currentDate.getMonth(), 1); // 1st of the current month
// Calculate the number of days since the starting date, excluding weekends and current day
var daysSinceStart = 0;
for (let i = 1; i < currentDayOfMonth; i++) {
let checkDate = new Date(currentDate.getFullYear(), currentDate.getMonth(), i);
if (checkDate.getDay() !== 0 && checkDate.getDay() !== 6) {
daysSinceStart++;
}
}
var minutesSinceStartOfDay = 0;
// if today is working day, and work time is over, count it as fully passed day
if (!isWeekend && isOutsideWorkingHours && currentHour >= endHour){
daysSinceStart++;
}
// If it's a weekend or outside working hours, show 'Counter paused.'
// else count minutes from start of a working day and show 'Counter starting...'
if (isWeekend || isOutsideWorkingHours) {
console.log('Counter paused.');
//return;
} else {
minutesSinceStartOfDay = (currentHour - startHour)*60 + currentMinutes;
console.log('Counter starting...');
}
// Calculate the initial value of the counter based on the number of previous days
// (endHour-startHour)*60 minutes in a day
// Initialize the counter with the initial value
var counter = daysSinceStart * (endHour-startHour)*60 + minutesSinceStartOfDay;
// Increment the counter every minute
var intervalId = setInterval(function() {
if (isWeekend || isOutsideWorkingHours) {
console.log('Counter:', counter);
return;
}
counter++;
console.log('Counter:', counter);
}, 60000); // 60000 milliseconds = 1 minute
// Display the counter value on page refresh
console.log('Counter:', counter);
}
// Start the counter
startCounter();
玩得开心! 对我来说很有趣。
关于javascript - 如何仅在工作时间增加计数器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76274168/