简体   繁体   English

无法调用函数以从另一个JavaScript文件传递变量值

[英]unable to call function to pass variable value from another javascript file

I used a specific question name to avoid duplicate question names, since I found some posters had the same problem and I have used the solutions offered but still unable to call the function from another javascript file and use alert() to see the variable value from another js file passed to html page. 我使用特定的问题名称以避免重复的问题名称,因为我发现一些张贴者也存在相同的问题,并且我使用了提供的解决方案,但仍然无法从另一个javascript文件调用该函数,并使用alert()从中查看变量值另一个js文件传递到html页面。

In my file.js file, I have a function to declare an array and put a global variable: 在我的file.js文件中,我有一个函数来声明一个数组并放置一个全局变量:

function MsgsArr(){
var Msgs = [
  "Msg1.",
  "Msg2.",
];
return Msgs;
}
var Msgs_Arr = MsgsArr();

followed by a function to create a random message and declare global variable: 然后是创建随机消息并声明全局变量的函数:

function CreateRandomMsg(){
var RandomMsg = Msgs_Arr[Math.floor(Math.random()* Msgs_Arr.length)];
return RandomMsg;
}
var Random_Msg = CreateRandomMsg();

Then it's the final function I want to achieve, to show the value based on the random message: 然后,这是我要实现的最终功能,以根据随机消息显示值:

function ValuesToCheck(){

if (Random_Msg == Msgs_Arr[0] || Random_Msg == Msgs_Arr[1]){
var check = facesDet.photos[0].tags[0].attributes.glasses.value
};
return check;
};

Since in the real case I have more than 2 messages, each 2 messages will share one value, the if statement will thus be longer. 因为在实际情况下,我有2条以上的消息,所以每2条消息将共享一个值,因此if语句将更长。 In my file1.html file, in a function I want to display the final value (true or false), and I use alert(ValuesToCheck()) to call the function, but it doesn't work. 在我的file1.html文件中,在一个函数中,我想显示最终值(true或false),并且我使用alert(ValuesToCheck())来调用该函数,但是它不起作用。 If I declare the value like var a = facesDet.photos[0].tags[0].attributes.glasses.value; 如果我声明类似var a = facesDet.photos[0].tags[0].attributes.glasses.value; in here and alert(a); 在这里并alert(a); it will work. 它会工作。 I'm not sure why things are not working when I'm trying to call the function from another js file, please help. 我不确定为什么当我尝试从另一个js文件调用该函数时,事情不起作用,请帮忙。 Sorry the question seems lengthy to read. 抱歉,这个问题似乎冗长。 Thanks for taking your time to read it. 感谢您抽出宝贵的时间阅读。

var facesDet = $.getJSON( APIdetect, function(facesDet) {
})

.done(function (facesDet, tid) {
    var tid = facesDet.photos[0].tags[0].tid;
    Load(tid);
    alert(ValuesToCheck());
});

From what I can see is that in your ValueToCheck function, RandomMsg and Msgs are uninitialised. 从我看到的是,在ValueToCheck函数中,RandomMsg和Msgs未初始化。 basically they are not global. 基本上,它们不是全球性的。 you need to use Random_Msg and Msgs_Arr instead. 您需要改用Random_Msg和Msgs_Arr。

Try this. 尝试这个。

function ValuesToCheck(){
   if (Random_Msg1 == Msgs_Arr[0] || Random_Msg2 == Msgs_Arr[1]){
      var check = facesDet.photos[0].tags[0].attributes.glasses.value
   };
   return check;
};

In your js file 在你的js文件中

var Random_Msg1 = CreateRandomMsg(),
    Random_Msg2 = CreateRandomMsg();

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

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