简体   繁体   English

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

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

I have created a function. 我创建了一个函数。
Which after resize the viewport, i want to remove the function completely. 调整视口大小后,我想完全删除该功能。
The off() method only remove the code inside scroll event. off()方法仅删除滚动事件内的代码。
When i'm resizing viewport, it's keep print out "this is still execute after resize". 当我调整视口大小时,它会一直打印“调整大小后仍会执行”。
Can i remove "myFunction" completely after resize the viewport with or without destroy my function? 调整视口大小后是否可以完全删除“ 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));
});

This is a work around for delay checking using timer because there is no exact resize event as you are looking for. 这是使用计时器进行延迟检查的一种解决方法,因为在您要查找的时候没有确切的调整大小事件。 If you stops for 500ms then resize function calls. 如果停止500毫秒,则调整函数调用的大小。 This may helps you 这可能对您有帮助

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");
});

You can simply overide it to 'undefined' after resize. 调整大小后,您可以简单地将其覆盖为“ 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;
} 
    });

Edit: 编辑:

What I suggest if what you need to reuse after it is to have the original function as backup. 我建议您是否需要在重用之后将原始功能用作备份。 Then use the "power" of JS where functions are variables . 然后使用JS的“功能”,其中函数是变量 Here the code you can try: 您可以在此处尝试以下代码:

    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;

to use the .off() function you must first instantiate the event with the .on() function follow example 若要使用.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