g2701_2800.s2725_interval_cancellation.solution.ts Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
The newest version!
// #Easy #2023_09_19_Time_51_ms_(98.87%)_Space_43.3_MB_(30.92%)
function cancellable(fn: Function, args: any[], t: number): Function {
fn(...args)
const timer = setInterval(() => {
fn(...args)
}, t)
return () => clearTimeout(timer)
}
/*
* const result = []
*
* const fn = (x) => x * 2
* const args = [4], t = 20, cancelT = 110
*
* const start = performance.now()
*
* const log = (...argsArr) => {
* const diff = Math.floor(performance.now() - start)
* result.push({"time": diff, "returned": fn(...argsArr)})
* }
*
* const cancel = cancellable(log, args, t);
*
* setTimeout(() => {
* cancel()
* }, cancelT)
*
* setTimeout(() => {
* console.log(result) // [
* // {"time":0,"returned":8},
* // {"time":20,"returned":8},
* // {"time":40,"returned":8},
* // {"time":60,"returned":8},
* // {"time":80,"returned":8},
* // {"time":100,"returned":8}
* // ]
* }, cancelT + t + 15)
*/
export { cancellable }
© 2015 - 2024 Weber Informatics LLC | Privacy Policy