, D3js, . , , . , . , "" , .
, .
var cachedThrottleFuncs = [],
minimumInterval = 200;
function throttle(func, obj, evt) {
var timeouttype = 0,
curFunc;
function lowerTimeoutType(f){
timeouttype=0;
if (curFunc !== undefined){
curFunc();
curFunc = undefined;
}
};
return cachedThrottleFuncs[ ~(
~cachedThrottleFuncs.indexOf(func) ||
~(
cachedThrottleFuncs.push(function(Evt) {
switch (timeouttype){
case 0:
++timeouttype;
func.call(Evt.target, Evt);
setTimeout(lowerTimeoutType, minimumInterval);
break;
case 1:
curFunc = func.bind(Evt.target, Evt);
Evt.preventDefault();
}
}) - 1
)
)];
};
function listen(obj, evt, func){
obj.addEventListener(evt, throttle(func, obj, evt));
};
function mute(obj, evt, func){
obj.removeEventListener(evt, throttle(func, obj, evt));
}
:
listen(document.body, 'scroll', function whenbodyscrolls(){
if (document.body.scrollTop > 400)
mute(document.body, 'scroll', whenbodyscrolls();
else
console.log('Body scrolled!')
});
, , , .
var minimumInterval = 200;
function throttle(func, obj, evt) {
var timeouttype = 0,
curEvt = null;
function lowerTimeoutType(f){
timeouttype=0;
if (curEvt !== null){
func(curEvt);
curEvt = null;
}
};
return function(Evt) {
switch (timeouttype){
case 0:
++timeouttype;
func(Evt);
setTimeout(lowerTimeoutType, minimumInterval);
break;
case 1:
curEvt = Evt;
Evt.preventDefault();
}
};
};
200 . , minimumInterval.