从另一个.js文件调用javascript函数

时间:2022-06-30 02:48:25

I have two external .js files. The first contains a function. The second calls the function.

我有两个外部的。js文件。第一个包含一个函数。第二个调用函数。

file1.js

file1.js

$(document).ready(function() {

    function menuHoverStart(element, topshift, thumbchange) {

        ... function here ...

    } 

});

file2.js

file2.js

$(document).ready(function() {

    setTimeout(function() { menuHoverStart("#myDiv", "63px", "myIMG"); },2000); 

});

The trouble is that this is not running the function. I need the two separate files because file2.js is inserted dynamically depending on certain conditions. This function works if I include the setTimeout... line at the end of file1.js

问题是这不是在运行函数。我需要两个单独的文件,因为文件2。根据某些条件动态插入js。如果我包含了setTimeout…行在file1.js的末尾

Any ideas?

什么好主意吗?

2 个解决方案

#1


43  

The problem is, that menuHoverStart is not accessible outside of its scope (which is defined by the .ready() callback function in file #1). You need to make this function available in the global scope (or through any object that is available in the global scope):

问题是,menuHoverStart在其范围之外(由文件#1中的.ready()回调函数定义)是不可访问的。您需要使此功能在全局范围内可用(或通过全局范围内可用的任何对象):

function menuHoverStart(element, topshift, thumbchange) {
    // ...
}

$(document).ready(function() {
    // ...
});

If you want menuHoverStart to stay in the .ready() callback, you need to add the function to the global object manually (using a function expression):

如果您希望menuHoverStart驻留在.ready()回调中,您需要手动将该函数添加到全局对象(使用函数表达式):

$(document).ready(function() {
    window.menuHoverStart = function (element, topshift, thumbchange) {
        // ...
    };
    // ...
});

#2


4  

You have declared menuHoverStart inside a function (the anonymous one you pass to the ready ready). That limits its scope to that function and you cannot call it from outside that function.

您已经在函数中声明了menuHoverStart(将匿名函数传递给ready)。这就把它的作用域限制在这个函数之外,你不能从这个函数外面调用它。

It doesn't do anything there, so there is no need to hold off on defining it until the ready event fires, so you could just move it outside the anonymous function.

它在那里什么都不做,所以在就绪事件触发之前没有必要对它进行定义,所以您可以将它移到匿名函数之外。

That said, globals are worth avoiding, so you might prefer to define a namespace (to reduce the risk of name collisions) and hang the function off that.

也就是说,全局变量是值得避免的,因此您可能更喜欢定义一个名称空间(以减少名称冲突的风险)并将函数挂起。

var MYNAMESPACE = {}; // In the global scope, not in a function
// The rest can go anywhere though
MYNAMESPACE.menuHoverStart = function (element, topshift, thumbchange) {

#1


43  

The problem is, that menuHoverStart is not accessible outside of its scope (which is defined by the .ready() callback function in file #1). You need to make this function available in the global scope (or through any object that is available in the global scope):

问题是,menuHoverStart在其范围之外(由文件#1中的.ready()回调函数定义)是不可访问的。您需要使此功能在全局范围内可用(或通过全局范围内可用的任何对象):

function menuHoverStart(element, topshift, thumbchange) {
    // ...
}

$(document).ready(function() {
    // ...
});

If you want menuHoverStart to stay in the .ready() callback, you need to add the function to the global object manually (using a function expression):

如果您希望menuHoverStart驻留在.ready()回调中,您需要手动将该函数添加到全局对象(使用函数表达式):

$(document).ready(function() {
    window.menuHoverStart = function (element, topshift, thumbchange) {
        // ...
    };
    // ...
});

#2


4  

You have declared menuHoverStart inside a function (the anonymous one you pass to the ready ready). That limits its scope to that function and you cannot call it from outside that function.

您已经在函数中声明了menuHoverStart(将匿名函数传递给ready)。这就把它的作用域限制在这个函数之外,你不能从这个函数外面调用它。

It doesn't do anything there, so there is no need to hold off on defining it until the ready event fires, so you could just move it outside the anonymous function.

它在那里什么都不做,所以在就绪事件触发之前没有必要对它进行定义,所以您可以将它移到匿名函数之外。

That said, globals are worth avoiding, so you might prefer to define a namespace (to reduce the risk of name collisions) and hang the function off that.

也就是说,全局变量是值得避免的,因此您可能更喜欢定义一个名称空间(以减少名称冲突的风险)并将函数挂起。

var MYNAMESPACE = {}; // In the global scope, not in a function
// The rest can go anywhere though
MYNAMESPACE.menuHoverStart = function (element, topshift, thumbchange) {