🌐 AI搜索 & 代理 主页
Skip to content

Commit dee0bb6

Browse files
committed
Merge pull request #54 from raphaeleidus/35_throttle_2
implement true throttling
2 parents bc0c066 + f44cbbc commit dee0bb6

File tree

4 files changed

+44
-23
lines changed

4 files changed

+44
-23
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ Type: `Number|String` Default: `250`
7373

7474
The throttle is managed by an internal function that prevents performance issues from continuous firing of `window.onscroll` events. Using a throttle will set a small timeout when the user scrolls and will keep throttling until the user stops. The default is `250` milliseconds.
7575

76+
#### debounce
77+
Type: `Boolean` Default: `true`
78+
79+
By default the throttling function is actually a [debounce](http://underscorejs.org/#debounce) function so that the checking function is only triggered after a user stops scrolling. To use traditional throttling where it will only check the images every `throttle` milliseconds, set `debounce` to `false`.
80+
7681
#### unload
7782
Type: `Boolean` Default: `false`
7883

dist/echo.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/*! echo.js v1.6.0 | (c) 2014 @toddmotto | https://github.com/toddmotto/echo */
22
(function (root, factory) {
33
if (typeof define === 'function' && define.amd) {
4-
define(factory);
4+
define(function() {
5+
return factory(root);
6+
});
57
} else if (typeof exports === 'object') {
68
module.exports = factory;
79
} else {
@@ -15,16 +17,22 @@
1517

1618
var callback = function () {};
1719

18-
var offset, poll, throttle, unload;
20+
var offset, poll, delay, useDebounce, unload;
1921

2022
var inView = function (element, view) {
2123
var box = element.getBoundingClientRect();
2224
return (box.right >= view.l && box.bottom >= view.t && box.left <= view.r && box.top <= view.b);
2325
};
2426

25-
var debounce = function () {
27+
var debounceOrThrottle = function () {
28+
if(!useDebounce && !!poll) {
29+
return;
30+
}
2631
clearTimeout(poll);
27-
poll = setTimeout(echo.render, throttle);
32+
poll = setTimeout(function(){
33+
echo.render();
34+
poll = null;
35+
}, delay);
2836
};
2937

3038
echo.init = function (opts) {
@@ -41,16 +49,17 @@
4149
l: optionToInt(opts.offsetLeft, offsetHorizontal),
4250
r: optionToInt(opts.offsetRight, offsetHorizontal)
4351
};
44-
throttle = optionToInt(opts.throttle, 250);
52+
delay = optionToInt(opts.throttle, 250);
53+
useDebounce = opts.debounce !== false;
4554
unload = !!opts.unload;
4655
callback = opts.callback || callback;
4756
echo.render();
4857
if (document.addEventListener) {
49-
root.addEventListener('scroll', debounce, false);
50-
root.addEventListener('load', debounce, false);
58+
root.addEventListener('scroll', debounceOrThrottle, false);
59+
root.addEventListener('load', debounceOrThrottle, false);
5160
} else {
52-
root.attachEvent('onscroll', debounce);
53-
root.attachEvent('onload', debounce);
61+
root.attachEvent('onscroll', debounceOrThrottle);
62+
root.attachEvent('onload', debounceOrThrottle);
5463
}
5564
};
5665

@@ -88,9 +97,9 @@
8897

8998
echo.detach = function () {
9099
if (document.removeEventListener) {
91-
root.removeEventListener('scroll', debounce);
100+
root.removeEventListener('scroll', debounceOrThrottle);
92101
} else {
93-
root.detachEvent('onscroll', debounce);
102+
root.detachEvent('onscroll', debounceOrThrottle);
94103
}
95104
clearTimeout(poll);
96105
};

dist/echo.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/echo.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(function (root, factory) {
22
if (typeof define === 'function' && define.amd) {
33
define(function() {
4-
return factory(root)
4+
return factory(root);
55
});
66
} else if (typeof exports === 'object') {
77
module.exports = factory;
@@ -16,16 +16,22 @@
1616

1717
var callback = function () {};
1818

19-
var offset, poll, throttle, unload;
19+
var offset, poll, delay, useDebounce, unload;
2020

2121
var inView = function (element, view) {
2222
var box = element.getBoundingClientRect();
2323
return (box.right >= view.l && box.bottom >= view.t && box.left <= view.r && box.top <= view.b);
2424
};
2525

26-
var debounce = function () {
26+
var debounceOrThrottle = function () {
27+
if(!useDebounce && !!poll) {
28+
return;
29+
}
2730
clearTimeout(poll);
28-
poll = setTimeout(echo.render, throttle);
31+
poll = setTimeout(function(){
32+
echo.render();
33+
poll = null;
34+
}, delay);
2935
};
3036

3137
echo.init = function (opts) {
@@ -42,16 +48,17 @@
4248
l: optionToInt(opts.offsetLeft, offsetHorizontal),
4349
r: optionToInt(opts.offsetRight, offsetHorizontal)
4450
};
45-
throttle = optionToInt(opts.throttle, 250);
51+
delay = optionToInt(opts.throttle, 250);
52+
useDebounce = opts.debounce !== false;
4653
unload = !!opts.unload;
4754
callback = opts.callback || callback;
4855
echo.render();
4956
if (document.addEventListener) {
50-
root.addEventListener('scroll', debounce, false);
51-
root.addEventListener('load', debounce, false);
57+
root.addEventListener('scroll', debounceOrThrottle, false);
58+
root.addEventListener('load', debounceOrThrottle, false);
5259
} else {
53-
root.attachEvent('onscroll', debounce);
54-
root.attachEvent('onload', debounce);
60+
root.attachEvent('onscroll', debounceOrThrottle);
61+
root.attachEvent('onload', debounceOrThrottle);
5562
}
5663
};
5764

@@ -89,9 +96,9 @@
8996

9097
echo.detach = function () {
9198
if (document.removeEventListener) {
92-
root.removeEventListener('scroll', debounce);
99+
root.removeEventListener('scroll', debounceOrThrottle);
93100
} else {
94-
root.detachEvent('onscroll', debounce);
101+
root.detachEvent('onscroll', debounceOrThrottle);
95102
}
96103
clearTimeout(poll);
97104
};

0 commit comments

Comments
 (0)