简体   繁体   English

未在javascript中调用函数

[英]Function not being called in javascript

I have the following code: 我有以下代码:

function isVisible(selector) {
    $(selector).is(':visible');
}

var isMapVisible;
var initialWindowWidth = window.innerWidth;
/* Window Resize Helper */
function handleWindowResize() {
    isMapVisible = isVisible('.search__map');
    if (window.innerWidth > 768) {
        var windowHeight = window.innerHeight,
        searchResultsHeight = windowHeight - $('.search__results').position().top,
        searchMapHeight = windowHeight - $('.search__map').position().top;
        $('.search__results').height(searchResultsHeight);
        $('.search__map').height(searchMapHeight);
        if (initialWindowWidth < 769) {
            /* 
                This is a hack to trigger the map to show up if initial windowWidth causes the map to hide. 
                it only triggers once.
            */
            resizeMap();
            map.setZoom(map.getZoom() - 1);
            initialWindowWidth = 1000;
        }
    } else {
        $('.search__results').height('auto');
    }
}

And in the function handleWindowResize() , I had a variable that I set globally as isMapVisible . 并且在handleWindowResize()函数中,我有一个全局设置为isMapVisible的变量。 and is calling another function isVisible() . 并正在调用另一个函数isVisible() I found out that when I replace that line of code with isMapVisible = $('.search__map').is(':visible') , I am able to get the correct result, but if my code is as copied above, I would get undefined. 我发现,当我用isMapVisible = $('.search__map').is(':visible')替换该行代码时,我可以得到正确的结果,但是如果我的代码如上复制,我会变得不确定。 Any idea why? 知道为什么吗?

That is because you are not returning anything in your function: 这是因为您没有在函数中返回任何内容:

function isVisible(selector) {
    return $(selector).is(':visible');
}

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

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