SOURCE

console 命令行工具 X clear

                    
>
console
// // 防抖
// function debounce (fn,delay) {
//     let timer = null;
//     return function(...args) {
//         if(timer) clearTimeout(timer);
//         timer = setTimeout(()=>{
//             fn.apply(this,args);
//         },delay)
//     }
// }

// // 节流
// function throttle(fn, delay) {
//     let last = 0;
//     return function(...args){
//         let now = new Date()
//         if(now-last>=delay) {
//             fn.apply(this.delay,args);
//             last = now;
//         }
//     }
    
// }
// const debounced = debounce(() => console.log("Click!"), 1000);
// const throttled = throttle(() => console.log("Scroll!"), 1000);

// 最长非重复子串
// const maxStringLen=(str)=>{
    
//     let left = 0;
//     let max = 0;
//     let substr = "";
//     let set = new Set();
//     for(let right = 0;right<str.length;right++) {
//         while(set.has(str[right])){
//             set.delete(str[left]);
//             left++;
//         }
//         set.add(str[right]);
//         max = Math.max(max,right-left+1);
//     }
//     for(let item of set.keys()) {
//         substr+= item;
//     }
//     return [max,substr];
// }

// const str = "aabcacdcdba"
// console.log(maxStringLen(str));

// 最大子数组和
// const maxSum=(arr)=>{
//     let max = 0;
//     let curMax = 0;
//     for(let i=0;i<arr.length;i++) {
//         curMax = Math.max(curMax+arr[i],arr[i]);
//         max = Math.max(max,curMax);
//     }
//     return max;
// }
// const arr=[-1,2,2,-2,3,-5,4,2]
// console.log(maxSum(arr));

const promiseall=(promises)=>{
    return new Promise((resolve,reject)=>{
        let result = [];
        let completed = 0;
        promises.forEach((item,index)=>{
            Promise.resolve(item).then(value=>{
                result[index] = value;
                completed++;
                if(completed===promises.length) resolve(result);
            }).catch(err=>reject(err))
        })
    })
}


function customPromiseAll(promises) {
  return new Promise((resolve, reject) => {
    const results = [];
    let completed = 0;

    if (promises.length === 0) {
      resolve([]);
      return;
    }

    promises.forEach((p, index) => {
      // 确保非 Promise 值也能处理
      Promise.resolve(p)
        .then(value => {
          results[index] = value;   // 保证顺序
          completed++;

          if (completed === promises.length) {
            resolve(results);
          }
        })
        .catch(err => {
          reject(err); // 一旦有失败,立即 reject
        });
    });
  });
}

const p1 = Promise.resolve(1);
const p2 = Promise.resolve(2);
const p3 = Promise.resolve(3);

// customPromiseAll([p1, p2, p3])
//   .then(res => console.log(res)) // [1, 2, 3]
//   .catch(err => console.error(err));

// promiseall([p1, p2, p3])
//   .then(res => console.log(res)) // [1, 2, 3]
//   .catch(err => console.error(err));


const wt=(parent,child,type,func)=>{
    parent.addEventListener(type,(event)=>{
        let target = event.target;
        while(target&&target!==parent) {
            if(target.matches(child)) {
                func.call(target,event)
                break;
            }
            target = target.parentElement;
        }
    })
}

function delegate(parent, selector, type, handler) {
  parent.addEventListener(type, function (event) {
    let target = event.target;

    while (target && target !== parent) {
      if (target.matches(selector)) {
        handler.call(target, event);
        break;
      }
      target = target.parentElement;
    }
  });
}

const ul = document.getElementById("list");

wt(ul, "li", "click", function (e) {
  console.log("You clicked:", this.textContent);
});
<button onclick="throttled()">123</button>

<ul id="list">
  <li>Item 1</li>
  <li>Item 2</li>
</ul>