简体   繁体   English

只调用一次函数?

[英]Only call function once?

So I have this javascript on a project I'm working on: 所以我在我正在研究的项目上有这个javascript:

<script type="text/javascript">

    document.getElementById('contact').onmouseover = function () 
    {
        var w = 130;
        function step() 
        {
            if (w < 250) 
            {
                middle.style.width = (w++) + "px";
                setTimeout(step, 5);
            }
        }
        setTimeout(step, 1500); 
    };

</script>

I want this to run only once. 我希望这只运行一次。 After it detects a mouseover, I want it to run the function and then never run again until the page refreshes. 在检测到鼠标悬停后,我希望它运行该功能,然后再次运行,直到页面刷新。 How would I accomplish this? 我怎么做到这一点?

I'd either use jQuery's one method or if you want to use 'plain' JavaScript you could just remove the event after the function has been triggered. 我要么使用jQuery的one方法,要么你想使用'普通'JavaScript,你可以在触发函数后删除事件。 Here's an example: 这是一个例子:

// Create a named function for the mouseover event
function myFunc() {
    // Remove the `myFunc` function event listener once `myFunc` is run
    document.getElementById('contact').removeEventListener('mouseover', myFunc, false);

    var w = 130;
    function step() {
        if (w < 250) {
            middle.style.width = (w++) + "px";
            setTimeout(step, 5);
        }
    }
    setTimeout(step, 1500);
};

// Add an event listener to run the `myFunc` function on mouseover
document.getElementById('contact').addEventListener('mouseover', myFunc, false);

Note that if you have to support IE8 (or even earlier), you need to use ...attachEvent("onmouseover", myFunc) and detachEvent("onmouseover", myFunc); 注意,如果你必须支持IE8(甚至更早),你需要使用...attachEvent("onmouseover", myFunc)detachEvent("onmouseover", myFunc); instead; 代替; you can tell by checking if the element has addEventListener : 你可以通过检查元素是否有addEventListener来判断:

var elm = document.getElementById('contact')
if (elm.addEventListener) {
    // Use addEventListener
}
else {
    // Use attachEvent
}

(Perhaps hidden away in a utility function.) (也许隐藏在实用功能中。)

All you need to do is remove the event listener from within the listener (so that it will stop listening to the event). 您需要做的就是从侦听器中删除事件侦听器(以便它将停止侦听事件)。 However, in order to remove the listener, you need a reference to it, so you can't do it with a predefined listener directly attached to mouseover . 但是,为了删除侦听器,您需要对它进行引用,因此您无法使用直接连接到mouseover的预定义侦听器来执行此操作。 Instead, use addEventListener to attach the listener, keep the returned reference and then use removeEventListener to remove the listener from within the callback. 相反,使用addEventListener来附加侦听器,保留返回的引用,然后使用removeEventListener从回调中删除侦听removeEventListener

var contact = document.getElementById('contact');

contact.addEventListener('mouseover', tehlistener);

function tehlistener() {
        // yada yada do whatever

        // ...

        // I think it's ok to use `this` here, but since this is so specific
        // its better to be specific about which listener you want removed
        contact.removeEventListener('mouseover', tehlistener);
    };

Here's a link to the lovely MDN article on addEventListener . 这是关于addEventListener的可爱MDN文章的链接。

If you are interested in using JQuery, there is a nice function called "one" that may be exactly what you need. 如果您对使用JQuery感兴趣,那么有一个很好的函数称为“one”,可能正是您所需要的。

http://api.jquery.com/one/ http://api.jquery.com/one/

Edit: Adding more code to show more of the solution: 编辑:添加更多代码以显示更多解决方案:

$( "#contact" ).one( "mouseover", function() {
    var w = 130;
    function step() 
    {
        if (w < 250) 
        {
            middle.style.width = (w++) + "px";
            setTimeout(step, 5);
        }
    }
    setTimeout(step, 1500); 
});

You can use a once function. 您可以使用once功能。

function once(fn){
   var called = false;
   return function(){
       if (called) {
           return;
       }
       called = true;
       return fn.apply(this, arguments);
   }
}

Example: 例:

something.onmouseover = once(function(){
    // this will happen only once
});

You could just overwrite the event handler 你可以只覆盖事件处理程序

<script type="text/javascript">
 document.getElementById('contact').onmouseover = function() {
 var w = 130;
 function step() {
  if (w < 250) {
    middle.style.width = (w++) + "px";
    setTimeout(step, 5);
  }
 }
 setTimeout(step, 1500); 
 this.onmouseover = null;//overwrite event handler with a blank callback
};
</script>

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

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