Last active
May 29, 2022 15:27
-
-
Save SalatielSauer/4fd345fb5f92fc94ae48459f7e8fd9ad to your computer and use it in GitHub Desktop.
A JavaScript function to calculate the best delay between a certain amount of tasks to evenly distribute them within a time range.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// organize tasks at equal intervals within a time range. | |
// receives two objects (start and end hours) and an integer (tasks) | |
function spreadTasks(start = {h: 0, m: 0}, end = {h: 23, m: 59}, tasks) { | |
let totalHour = end.h - start.h; | |
let totalMinute = end.m - start.m; | |
let totalTime = (totalHour*60)+totalMinute; | |
let taskTable = []; | |
for (let i = 0; i <= totalTime; i+=(totalTime/tasks)) { | |
let taskTime = ((start.h*60)+start.m)+i; | |
let taskHour = Math.floor(taskTime/60); | |
let taskMinute = Math.ceil(((taskTime/60)-taskHour)*60); | |
let taskSecond = Math.floor((taskMinute-(((taskTime/60)-taskHour)*60))*60); | |
taskTable.push({ | |
"h": taskHour, | |
"m": taskMinute, | |
"s": taskSecond | |
}); | |
}; | |
return { | |
"delay": { | |
"m": Math.floor(totalTime/tasks), | |
"s": Math.floor((Math.ceil(totalTime/tasks)-(totalTime/tasks))*60) | |
}, | |
"schedule": taskTable | |
}; | |
} | |
spreadTasks({h: 7, m: 20}, {h: 12, m: 30}, 15); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment