Skip to content

2725. Interval Cancellation

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
type JSONValue =
  | null
  | boolean
  | number
  | string
  | JSONValue[]
  | { [key: string]: JSONValue };
type Fn = (...args: JSONValue[]) => void;

function cancellable(fn: Fn, args: JSONValue[], t: number): Function {
  fn(...args);
  const timer = setInterval(() => fn(...args), t);
  return function () {
    clearInterval(timer);
  };
}