在另一个函数jquery [duplicate]的参数中使用返回值

时间:2021-04-21 16:39:11

This question already has an answer here:

这个问题在这里已有答案:

I trie to get the result of this function :

我试图得到这个函数的结果:

var isLaptop = false;
function is1024up() {   
    $(window).on('load resize', function() {
        isLaptop = $(window).width() > 1024;
    });
    return isLaptop;
} 

lap = is1024up(); // I put the result of my function in a global

I would like to use the result of this function on parameter of another function

我想在另一个函数的参数上使用这个函数的结果

function test(lap){
    console.log(lap); //the result don't change on resize
    if (lap) {
      // my code here
    }

}

My issue is on the resize function : I can get the result on is1024up() when the page load but this result isn't uptable on resize.

我的问题是关于resize函数:我可以在页面加载时得到is1024up()的结果,但是这个结果在调整大小时不能提升。

Any idea ?

任何的想法 ?

1 个解决方案

#1


0  

You can't do it with return. Use the resize event to just update the variable. Use the isLaptop variable just everywhere to read the current state.

你不能用回报来做。使用resize事件只更新变量。在任何地方使用isLaptop变量来读取当前状态。

var isLaptop = false;

$(window).on('load resize', function() {
    isLaptop = $(window).width() > 1024;
});

function test() {
    console.log(isLaptop);
}

#1


0  

You can't do it with return. Use the resize event to just update the variable. Use the isLaptop variable just everywhere to read the current state.

你不能用回报来做。使用resize事件只更新变量。在任何地方使用isLaptop变量来读取当前状态。

var isLaptop = false;

$(window).on('load resize', function() {
    isLaptop = $(window).width() > 1024;
});

function test() {
    console.log(isLaptop);
}