-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c2a5ff6
commit b6205e5
Showing
5 changed files
with
78 additions
and
73 deletions.
There are no files selected for viewing
File renamed without changes
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,30 @@ | ||
/** | ||
* 添加immediate参数,让函数能够立刻执行,仅当事件停止触发n秒后,才能重新触发 | ||
* 添加函数返回值 | ||
*/ | ||
var count = 1; | ||
var container = document.getElementById('container'); | ||
|
||
function getUserAction(e) { | ||
container.innerHTML = count++; | ||
}; | ||
|
||
container.onmousemove = debounce(getUserAction, 1000, true); | ||
|
||
// 第五版 | ||
function debounce(func, wait, immediate) { | ||
|
||
var timeout, result; | ||
|
||
return function () { | ||
var context = this; | ||
var args = arguments; | ||
|
||
if (timeout) clearTimeout(timeout); | ||
if (immediate) { | ||
// 如果已经执行过,不再执行 | ||
var callNow = !timeout; | ||
timeout = setTimeout(function(){ | ||
timeout = null; | ||
}, wait) | ||
if (callNow) result = func.apply(context, args) | ||
} | ||
else { | ||
timeout = setTimeout(function(){ | ||
result = func.apply(context, args) | ||
}, wait); | ||
} | ||
|
||
|
||
return result; | ||
} | ||
var timeout, result; | ||
|
||
return function () { | ||
var context = this; | ||
var args = arguments; | ||
|
||
if (timeout) clearTimeout(timeout); | ||
if (immediate) { | ||
// 如果已经执行过,不再执行 | ||
var callNow = !timeout; | ||
timeout = setTimeout(function(){ | ||
timeout = null; | ||
}, wait) | ||
if (callNow) result = func.apply(context, args) | ||
} | ||
else { | ||
timeout = setTimeout(function(){ | ||
func.apply(context, args) | ||
}, wait); | ||
} | ||
return result; | ||
} | ||
} |
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