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
import { useEffect, useRef } from 'react' | |
function useInterval(callback, delay) { | |
const savedCallback = useRef(callback) | |
// Remember the latest callback if it changes. | |
useEffect(() => { | |
savedCallback.current = callback | |
}, [callback]) |
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
class PriorityQueue { | |
#top = 0; | |
#heap; | |
#comparator; | |
constructor(comparator = (a, b) => a > b) { | |
this.#heap = []; | |
this.#comparator = comparator; | |
} | |
#parent(i) { |
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
// 方法一,非异步方式 | |
const supportWebP = (function () { | |
var canvas = typeof document === 'object' ? document.createElement('canvas') : {} | |
canvas.width = canvas.height = 1 | |
return canvas.toDataURL ? canvas.toDataURL('image/webp').indexOf('image/webp') === 5 : false | |
})() | |
// 异步方式获取,比较适合系统中存在一个系统变量来保存结果 | |
window.isSupportWebp = false; // 是否支持 | |
(function() { |
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
let storage = window.localStorage; | |
/** | |
* 判断是否为 JSON 对象 | |
* @param obj | |
* @returns {boolean} | |
*/ | |
function isJSON(obj) { | |
obj = JSON.stringify(obj); | |
return /^\{[\s\S]*\}$/.test(obj); |