zoukankan      html  css  js  c++  java
  • [Javascript] Coding interview problem: Scheduler functional way

    Implement a job scheduler which takes in a function f and an integer n, and calls f after nmilliseconds

    function curry (fn) {
      const arity = fn.length;
      return function $curry(...args) {
        if (args.length < arity) {
            return $curry.bind(null, ...args);
        }
        
        return fn.call(null, ...args);
      }
    }
    
    const setScheduler = curry((ms, fn) => {
      return setTimeout(() => fn(), ms) 
    });
    
    const halfSecond = setScheduler(500);
    
    console.log('before'); 
    halfSecond(() => console.log('Timeup')); 
    setTimeout(() => console.log('after'), 600); 
    // |A          A: before
    // |-----B     B: Timeup
    // |------C    C: after
  • 相关阅读:
    隔离级别
    cookie
    session
    正则表达式
    hello2源代码解析
    servlet_filter简介
    web.xml
    Annotations
    Java design patterna
    CDI Features
  • 原文地址:https://www.cnblogs.com/Answer1215/p/10520694.html
Copyright © 2011-2022 走看看