繁体   English   中英

我如何在(window).resize之后完全删除jquery函数

[英]How can i completely remove a jquery function after (window).resize

我创建了一个函数。
调整视口大小后,我想完全删除该功能。
off()方法仅删除滚动事件内的代码。
当我调整视口大小时,它会一直打印“调整大小后仍会执行”。
调整视口大小后是否可以完全删除“ myFunction”,而不会破坏我的功能?

function myFunction(ele) {
    myArray = [];
    this.num1 = $.each(ele, function(){
        var posx = ele.offset().top;
        myArray.push(pos);
        console.log('this is still execute after resize');
        $(window).on('scroll', function(){
            // abcxyz
        });
    });
}

var target = $('p');
var run = new myFunction(target);

$(window).on('resize',function(){
    console.log("resized");
    $(window).off('scroll', myFunction(target));
});

这是使用计时器进行延迟检查的一种解决方法,因为在您要查找的时候没有确切的调整大小事件。 如果停止500毫秒,则调整函数调用的大小。 这可能对您有帮助

function myFunction(ele) {
myArray = [];
this.num1 = $.each(ele, function(){
    var posx = ele.offset().top;
    myArray.push(pos);
    console.log('this is still execute after resize');
    $(window).on('scroll', function(){
        // abcxyz
    });
});
}

var target = $('p');
var run = new myFunction(target);

var listenForStop = (function () {
    var timers = {};
    return function (callback, ms, uId) {
        if (timers[uId]) {
            clearTimeout(timers[uId]);
        }
        timers[uId] = setTimeout(callback, ms);
    };
})();

$(window).on('resize', function () {
    listenForStop(function () {
        $(window).off('scroll', myFunction(target));
    }, 500, "unique");
});

调整大小后,您可以简单地将其覆盖为“ undefined”。

function myFunction(ele) {
    myArray = [];
    this.num1 = $.each(ele, function(){
        var posx = ele.offset().top;
        myArray.push(pos);
        console.log('this is still execute after resize');
        $(window).on('scroll', function(){
            // abcxyz
        });
    });
}

var target = $('p');
var run = new myFunction(target);



  $(window).on('resize',function(){
        console.log("resized");
     if (typeof(myFunction) == "function"){
myFunction = undefined;
} 
    });

编辑:

我建议您是否需要在重用之后将原始功能用作备份。 然后使用JS的“功能”,其中函数是变量 您可以在此处尝试以下代码:

    function myFunctionBackup(ele) {
    myArray = [];
    this.num1 = $.each(ele, function(){
        var posx = ele.offset().top;
        myArray.push(pos);
        console.log('this is still execute after resize');
        $(window).on('scroll', function(){
            // abcxyz
        });
    });
}

var target = $('p');
var myFunction = myFunctionBackup;
myFunction(target);

  $(window).on('resize',function(){
        console.log("resized");
     if (typeof(myFunction) == "function"){
myFunction = undefined;
} 
    });

// When you want to return original simply do:
myFunction = myFunctionBackup;

若要使用.off()函数,必须首先使用.on()函数实例化该事件,并遵循以下示例

function myFunction(ele) {
    myArray = [];
    this.num1 = $.each(ele, function(){
        var posx = ele.offset().top;
        myArray.push(pos);
        console.log('this is still execute after resize');
        $(window).on('scroll', function(){
            // abcxyz
        });
    });
}

var target = $('p');
var run = new myFunction(target);

$(function(){
   $(window).on('scroll', myFunction(target));
});

$(window).on('resize',function(){
    console.log("resized");
    $(window).off('scroll', myFunction(target));
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM