简体   繁体   English

是否可以同时从两个调用函数中调用单个参数的同一个函数?

[英]Is it possible to call a same function with single parameter from two calling function at same time?

I am developing an HTML5/JavaScript app. 我正在开发HTML5 / JavaScript应用。

I have a function named letter(x) it has a single parameter and I want to call it from two different places at a time. 我有一个名为letter(x)的函数,它具有单个参数,我想一次从两个不同的地方调用它。 Is this possible? 这可能吗?

I call it from an HTML where I have two image buttons with onclick functions. 我从HTML调用它,其中有两个带有onclick函数的图像按钮。 When I click the two image one by one it should pass and give a result 当我一张一张地单击两个图像时,它应该通过并给出结果

My code is below: Function code in JavaScript 我的代码如下:JavaScript中的功能代码

function letter(x) {

    if (x==1&&a) {

        document.getElementById('audio2').play();
        document.getElementById('ka').style.visibility = "true"

    }
}

HTML code from which I call 我从中调用的HTML代码

<img src="images/mei/off/01.png" id="ik" onclick="letter(1);"/>

<img src="images/uyir/off/01.png" id="ah" onclick="letter(a);"/>

In the above when I tried to pass the two function parameters at same time I can't pass to it. 在上面,当我尝试同时传递两个函数参数时,无法传递给它。 Is it possible? 可能吗? If it's possible, how can it be done? 如果有可能,该怎么办?

You cannot do what you are trying to achieve. 您无法做您想达到的目标。 When you call the function on clicking the first button, it will be executed with the '1' parameter and the condition will be false. 当您单击第一个按钮时调用该函数时,它将使用“ 1”参数执行,条件将为假。 Same with the second button and the paraneter 'a'. 与第二个按钮和参数“ a”相同。 I would do something like this: 我会做这样的事情:

var lastCallParameter = '';

function letter(x)
{
   if((lastCallParameter  == '1' && x == 'a') || (lastCallParameter == 'a' && x == '1'))
   {
      document.getElementById('audio2').play();
      document.getElementById('ka').style.visibility = "true"
   }
   lastCallParameter   = x;
}

On that code, the function core will be executed only if you click on the 2 buttons one after the other. 在该代码上,仅当您一个接一个地单击两个按钮时,功能内核才会执行。 If you click one one button only, it won't do anything. 如果仅单击一个按钮,它将不会执行任何操作。

And your html code should be: 并且您的html代码应为:

<img src="images/mei/off/01.png" id="ik" onclick="letter(1);"/>
<img src="images/uyir/off/01.png"  id="ah" onclick="letter('a');"/>

Unless you have a variable named 'a' somewhere in your code. 除非您在代码中某处有一个名为“ a”的变量。 In that case, you should remove the simple quote around 'a'. 在这种情况下,您应该删除'a'周围的简单引号。 In your code, I can't understand if 'a' is a character or a variable defined somewhere else. 在您的代码中,我无法理解“ a”是字符还是其他地方定义的变量。

Plase note that, to avoid using a global variable (which is a bad thing), you could do this : 请注意,为避免使用全局变量(这是一件坏事),可以这样做:

var letter = (function(){
   var lastCallParameter = '';

   return function(x)
    {
       if((lastCallParameter  == '1' && x == 'a') || (lastCallParameter == 'a' && x == '1'))
       {
          document.getElementById('audio2').play();
          document.getElementById('ka').style.visibility = "true"
       }
       lastCallParameter   = x;
    }
})();

But it would need you to know about closures and scopes to understand that code. 但这需要您了解闭包和范围才能理解该代码。 If you don't, you can use the first version. 如果没有,则可以使用第一个版本。

Your function is checking that x is equal to the number 1, and that a is truthy, but there is no a in that scope. 您的函数正在检查x等于数字1,并且a是正确的,但在该范围内没有a Perhaps you want to know that x is either the number 1 or the letter a? 也许您想知道x是数字1 还是字母a?

function letter(x) {
    if (x == 1 || x == 'a') {
        document.getElementById('audio2').play();
        document.getElementById('ka').style.visibility = "true"
    }
}

If you want to know that x is 1 and a is truthy, you need to supply it in your args: 如果您想知道x为1且a为真,则需要在args中提供它:

function letter(x, a) {
    if (x == 1 && a) {
        document.getElementById('audio2').play();
        document.getElementById('ka').style.visibility = "true"
    }
}

<img src="images/mei/off/01.png" id="ik" onclick="letter(1, true);"/>
<img src="images/uyir/off/01.png"  id="ah" onclick="letter(1, 'still true');"/>

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

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