Created
September 10, 2024 19:59
-
-
Save bazuka5801/5f31064e4fdf669d3279656b770c9650 to your computer and use it in GitHub Desktop.
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
// progress in 0..1 range, time is epoch seconda | |
function estimateProgressRemainTime(progressLast, timeLast, progressCurrent, timeCurrent) { | |
// Calculate the time taken for the progress made | |
const timeTaken = timeCurrent - timeLast; // time in seconds | |
const progressMade = progressCurrent - progressLast; | |
// Avoid division by zero or negative progress | |
if (progressMade <= 0) { | |
return "No progress made or invalid input."; | |
} | |
// Calculate the remaining time | |
const speed = timeTaken/(progressMade*100); | |
const remainProgress = 1-progressCurrent; | |
return speed*remainProgress*100; | |
} | |
// Tests | |
test('remainTime', () => { | |
const timeCurrent = Date.now() / 1000; | |
const timeLast = timeCurrent - 2; | |
const lastProgress = 0.25; | |
const currentProgress = 0.5; | |
// Было 25% секунды 2 назад, сейчас 50%, через сколько секунд будет 100% | |
const estimateTime = estimateProgressRemainTime(lastProgress, timeLast, currentProgress, timeCurrent) | |
console.log(estimateTime) | |
expect(estimateTime).toBe(4); | |
}); | |
test('remainTime2', () => { | |
const timeCurrent = Date.now() / 1000; | |
const timeLast = timeCurrent - 2; | |
const lastProgress = 0.2; | |
const currentProgress = 0.4; | |
// Было 20% секунды 2 назад, сейчас 40%, через сколько секунд будет 100% | |
const estimateTime = estimateProgressRemainTime(lastProgress, timeLast, currentProgress, timeCurrent) | |
console.log(estimateTime) | |
expect(estimateTime).toBe(6); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment