/
window-scroll-event.html
60 lines (50 loc) · 1.31 KB
/
window-scroll-event.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<!doctype html>
<html lang="en">
<head>
<title>JavaScript Patterns</title>
<meta charset="utf-8">
</head>
<body>
<script>
/* Title: window scroll event
* Description: avoid attaching handlers to the window scroll event
*/
// antipattern
$(window).scroll(function () {
$('.foo').something();
});
// preferred
var outerPane = $details.find(".details-pane-outer"),
didScroll = false;
$(window).scroll(function () {
didScroll = true;
});
setInterval(function () {
if (didScroll) {
didScroll = false;
// Check your page position and then
// Load in more results
// outerPane.html();
}
}, 250);
// preferred v2, timeout instead of interval - no unnecessary ticks
var scrollTimeout; // global for any pending scrollTimeout
var outerPane = $details.find(".details-pane-outer");
$(window).scroll(function () {
if (scrollTimeout) {
// clear the timeout, if one is pending
clearTimeout(scrollTimeout);
scrollTimeout = null;
}
scrollTimeout = setTimeout(scrollHandler, 250);
});
scrollHandler = function () {
// Check your page position and then
// Load in more results
// outerPane.html();
};
// References
// http://ejohn.org/blog/learning-from-twitter/
</script>
</body>
</html>