chenhuiYj / chenhuiYj/note

防抖与节流

Open
#44 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
HTML
Stars
6
Forks
0
PR merge metrics
No merged PRs in 30d

Description

防抖:触发高频事件后 n 秒内函数只会执行一次,如果 n 秒内高频事件再次被触发,则重新计算时间。作用:防止事件连续或高频触发,让它只触发一次或者最后一次。
function debounce (fn, delay){
	let timer=null
	return function(){
		if(timer){
			clearTimeout(timer)
		}
		timer=setTimeout(()=>{
			fn.apply(this,arguments)
			timer=null
		},delay)
	}
}

let test=debounce(()=>{console.log(1)},5000)
节流:高频事件触发,但在 n 秒内只会执行一次,所以节流会稀释函数的执行频率。作用:降低事件触发的频率,比如2s 内最多执行一次。
function throttle(fn, wait){
	let last=0
	return function(){
		let now=+new Date()
		if(now-last>=wait){
			fn.apply(this,arguments)
			last=now
		}
	}
}

let test=throttle(()=>{console.log(2)},2000)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue mentions no target file, test, or entry point. First inspect the repository structure to find where JavaScript notes are kept; the work is done only when the intended debounce and throttle material is added there, but the issue does not define a specific location or acceptance check.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.