如何去除一个事件? (错误的语法?) [英] How to debounce an event? (wrong Syntax?)
本文介绍了如何去除一个事件? (错误的语法?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我玩这个代码但没有任何结果。去抖动没有完成。
在这种情况下如何调用debouncing-function?
通过调用它们,debouncing函数可以正常工作: var resizeIframeAd = debounce(function(){...}
(函数(窗口,文档, undefined){
'use strict';
/ *
* Global api。
* /
var adTech = window.adTech = {
get: function(){
return _instance;
},
//主要入口点。
init:function(options){
return _instance || new ADTECH(options );
}
};
/ **
*构造函数。
* /
var ADTECH =函数(选项){
var defaultOptions = {
adID:'5202402',
hiddenClassName:'hidden'
};
this.options = this.extend(options,def aultOptions);
this.makeAd();
_instance = this;
return _instance;
}
ADTECH.prototype = {
extend:function(source,target){
if(source == null){return target; }
for(var k in source){
if(source [k]!= null&& target [k]!== source [k]){
target [k ] =来源[k];
}
}
返回目标;
},
log:function(msg){
if(window.console){
console.log(msg);
}
},
//反弹功能
debounce:function(func,wait,immediate){
var timeout;
return function(){
var context = this,args = arguments;
var later = function(){
timeout = null;
if(!immediate)func.apply(context,args);
};
var callNow = immediate&& !超时;
clearTimeout(超时);
timeout = setTimeout(稍后,等待);
if(callNow)func.apply(context,args);
};
},
//事件处理程序
addEvent:function(elem,type,eventHandle){console.log(adEvent is undefined:,eventHandle);
if(elem == null || typeof(elem)=='undefined')return;
if(elem.addEventListener){
elem.addEventListener(type,eventHandle,false);
} else if(elem.attachEvent){
elem.attachEvent(on+ type,eventHandle);
} else {
elem [on+ type] = eventHandle;
}
},
//去辩论这个功能我称之为var resizeIframeAd = debounce(function(){怎么去这里?
resizeAd:function(invokeLater){
//这是基于调用对象显式设置的。
var obj = this;
var helper = function(){
obj.log(resizeAd2 done。);
// var ad = document.getElementById(obj.options.adID); //obj.log(ad);
obj。 debounce(function(){
console.log(请去抖动这个函数);
},250)
};
//如果invokeLater是假值,那么立即调整大小
//如果它是真正的返回帮助器,那么可以将它指定为
//事件处理程序
返回invokeLater?helper:helper();
},
//插入广告
makeAd:function (){
this.addEvent(window,resize,this.resizeAd(true));
}
}
// Singleton
var _instance;
}(窗口,文档));
var API = adTech.init();
解决方案
我想你要去除你的 helper
function:
var helper = this.debounce(function(){
obj.log(resizeAd2 done。);
// var ad = document.getElementById(obj.options.adID); //obj.log(ad);
console.log(请辩论这个函数);
},250);
I'am a little bit confused. I would like do debounce the function resizeAd on windows-resize. I played with this code but without any result. The debouncing is not done. How I have to call the debouncing-function in this case?
The debouncing function works fine by calling them like this: var resizeIframeAd = debounce(function() {... }
(function(window, document, undefined) {
'use strict';
/*
* Global api.
*/
var adTech = window.adTech = {
get: function() {
return _instance;
},
//Main entry point.
init: function(options) {
return _instance || new ADTECH(options);
}
};
/**
* Constructor.
*/
var ADTECH = function(options) {
var defaultOptions = {
adID : '5202402',
hiddenClassName : 'hidden'
};
this.options = this.extend(options, defaultOptions);
this.makeAd();
_instance = this;
return _instance;
}
ADTECH.prototype = {
extend: function(source, target) {
if (source == null) { return target; }
for (var k in source) {
if(source[k] != null && target[k] !== source[k]) {
target[k] = source[k];
}
}
return target;
},
log: function(msg){
if(window.console){
console.log(msg);
}
},
// Debounce function
debounce: function(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
},
// event Handler
addEvent: function (elem, type, eventHandle) { console.log("adEvent is undefined: ",eventHandle);
if (elem == null || typeof(elem) == 'undefined') return;
if (elem.addEventListener) {
elem.addEventListener(type, eventHandle, false);
} else if (elem.attachEvent) {
elem.attachEvent("on" + type, eventHandle);
} else {
elem["on" + type] = eventHandle;
}
},
//to debounce this fucntion I call them like this var resizeIframeAd = debounce(function() { HOW TO DO THAT HERE?
resizeAd: function(invokeLater) {
// this is explicitly set based on the calling object.
var obj = this;
var helper = function () {
obj.log("resizeAd2 done.");
//var ad = document.getElementById(obj.options.adID); //obj.log(ad);
obj.debounce(function() {
console.log("please debounce this function");
}, 250)
};
// if invokeLater is a falsey value do the resizing right away
// if it is truthy return helper so that it can be assigned as
// an event handler
return invokeLater ? helper : helper();
},
// insert ad
makeAd: function () {
this.addEvent(window, "resize", this.resizeAd(true));
}
}
// Singleton
var _instance;
}(window, document));
var API = adTech.init();
解决方案
I think you want to debounce your helper
function:
var helper = this.debounce(function () {
obj.log("resizeAd2 done.");
//var ad = document.getElementById(obj.options.adID); //obj.log(ad);
console.log("please debounce this function");
}, 250);
这篇关于如何去除一个事件? (错误的语法?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文