function throttle(fn, interval) {
var enterTime = 0;
var gapTime = interval || 300 ;
return function() {
var context = this;
var backTime = new Date();
if (backTime - enterTime > gapTime) {
fn.call(context,arguments);
enterTime = backTime;
}
};
}
function debounce(fn, interval) {
var timer;
var gapTime = interval || 1000;
return function() {
clearTimeout(timer);
var context = this;
var args = arguments;
timer = setTimeout(function() {
fn.call(context,args);
}, gapTime);
};
}
export default {
throttle,
debounce
};
import tool from "../../static/js/tool.js";
Page({
data:{
win_scrollTop:0
},
onPageScroll: tool.throttle(function(msg){
this.setData({
win_scrollTop: msg[0].scrollTop
});
}),
gotoUnlock: tool.debounce(function() {
this.saveUserInfo();
}),
saveUserInfo:function(){
console.log(111)
}
})
如果是typescript呢 这个this就无法被推断为Page实例或者Component实例了