Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/455.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使函数在javascript中只运行一次_Javascript_Events - Fatal编程技术网

如何使函数在javascript中只运行一次

如何使函数在javascript中只运行一次,javascript,events,Javascript,Events,如何使功能每个按钮只运行一次? 如果单击“单击我”只工作一次,其他按钮也是如此 为了不放太多代码,我举了一个例子: 更改onclick处理程序,以便函数可以引用所单击的元素 <input type="button" value="click me" onclick="hello.call(this)"><br> <input type="button" value="click me1" onclick="hello.call(this)"><br&g

如何使功能每个按钮只运行一次? 如果单击“单击我”只工作一次,其他按钮也是如此 为了不放太多代码,我举了一个例子:

更改onclick处理程序,以便函数可以引用所单击的元素

<input type="button" value="click me" onclick="hello.call(this)"><br>
<input type="button" value="click me1" onclick="hello.call(this)"><br>
<input type="button" value="click me2" onclick="hello.call(this)">

您只需删除onclick

<html>
<head>
    <title></title>
</head>
<body>
    <input type="button" value="click" onclick="hello(this)"><br>
    <input type="button" value="click1" onclick="hello(this)"><br>
    <input type="button" value="click2" onclick="hello(this)">
<script>
       function hello(btn){ 
           alert("hello");
           btn.onclick = function(){};
    }
</script>
</body>
</html>

<> >如果在脚本中添加事件侦听器,则更容易管理。它也可以考虑将行为与演示文稿分开的好实践:

演示:

单击button调用下面的函数,并将按钮id或名称作为参数


为按钮数量添加上述条件虽然上述场景和答案都非常特定于单击处理程序,但对于如何使函数只运行一次的原始问题的答案通常使用包装函数完成,类似于下划线JS。once方法:

上面的实现只允许调用原始函数一次,并且它将传递后面调用的上下文和参数。这将被用作:

var oneTimeFn = once(function() {
  console.log('I was called.');
});

oneTimeFn();
//-> I was called.

oneTimeFn();
//-> undefined

hello.call是否对元素起作用?我希望您能这样做,并使用函数hellobut{but.onclick=null}-ah,正如我看到的公认答案一样does@mplungjan:是,使用.callthis将函数的this值设置为当前this。我更喜欢它而不是hellothis,因为它允许函数像典型的事件处理程序一样运行,其中这是对元素的引用。您还可以执行hello.callthis,event,它将传递事件对象。。。即使在IE中,我每天都能学到新东西,真是太神奇了。。。我这样问是因为我拒绝了有人试图用编辑来修复你的代码。@mplungjan:是的,我注意到了。谢谢你拒绝了我几乎不再使用内联处理程序了,但这是一件好事。我第一次看到它。
<html>
<head>
    <title></title>
</head>
<body>
    <input type="button" value="click" onclick="hello(this)"><br>
    <input type="button" value="click1" onclick="hello(this)"><br>
    <input type="button" value="click2" onclick="hello(this)">
<script>
       function hello(btn){ 
           alert("hello");
           btn.onclick = function(){};
    }
</script>
</body>
</html>
<input type="button" value="click">
<input type="button" value="click1">
<input type="button" value="click2">​

<script>
var inputs = document.getElementsByTagName('input');
for(var i=0; i<inputs.length; i++) {
    inputs[i].onclick = function() {
        hello();
        this.onclick = null; // reset the handler
    }
}
function hello() {
    alert('hello';
}
</script>
    <script>
       function hello(caller){
          if (caller == 'button1' && $("#button1clicked").val() != '1')
          {
         // Your code to execute for the function
         alert("hello");
       // set value for button1clicked
       $("#button1clicked").val("1");
       }else {
       // do nothing
       }

     }
     </script>
function once(fn) {
  var called = false;
  return function() {
    if (!called) {
      called = true;
      fn.apply(this, arguments);
    }
  }
}
var oneTimeFn = once(function() {
  console.log('I was called.');
});

oneTimeFn();
//-> I was called.

oneTimeFn();
//-> undefined