简体   繁体   English

仅在事件触发时调用函数

[英]Call a function only when fired by an event

I have the following function: 我有以下功能:

format : function(){
   //function body
   if(condition){
       //execute this condition only on focus event 
   }
}

This function is being called on page load and on the focus of an input element. 在页面加载和输入元素的焦点上调用此函数。 I have a condition within this function that I would like to be called only when this function is called manually from the focus event and not on page load. 我在这个函数中有一个条件,我只想在从焦点事件手动调用此函数而不是在页面加载时调用此函数。 Is there a way to achieve this? 有没有办法实现这个目标?

There are cases sometimes when the 'condition' in the if statement is true on page load as well. 有时候if语句中的'condition'在页面加载时也是如此。 Any help will be appreciated. 任何帮助将不胜感激。 Thanks in advance. 提前致谢。

Separate into two functions: 分为两个功能:

format: function() {
    //Do stuff for focus
}
onload: function() {
    //Do stuff for onload
    this.format();
}

You can just check for the presence of an event parameter: 您只需检查是否存在event参数:

format: function(event) {
    if (event) {
        // called form an event
    } else {
        // called manually (without a parameter)
    }
}

For further robustness, check the event.type parameter - it'll contain the name of the event that fired. 为了进一步增强健壮性,请检查event.type参数 - 它将包含触发事件的名称。

Just use event parameter. 只需使用事件参数。

format : function(e){
   //function body
   if(e && e.type == 'focus'){
       //execute this condition only on focus event 
   }
}

Demo: http://jsfiddle.net/mbL5ohwr/ 演示: http//jsfiddle.net/mbL5ohwr/

you can just was an argument when calling manually on focus and check for no of argument like with special keyword "arguments", when you call it on page load without an argument then length will be zero else it wont be zero , so condition will be true for focus functionality only 你可以只是在焦点上手动调用并检查没有像特殊关键字“arguments”这样的参数时的参数,当你在没有参数的页面加载上调用它时,长度将为零,否则它不会为零,所以条件将是仅适用于焦点功能

format : function(){

 if(arguments.length){
   //execute this condition only on focus event 
 }
 }

You can check whether the element has focus, using for example this === document.activeElement or $(this).is(':focus') . 您可以使用例如this === document.activeElement$(this).is(':focus')来检查元素是否具有焦点。 If the function is fired through the focus event, these will return true and if they're called from the onload event, these will return false . 如果函数是通过焦点事件触发的,则这些函数将返回true ,如果它们是从onload事件调用的,则它们将返回false

I hope this helped you! 我希望这对你有所帮助!

just thought to share: This is what worked for me from the solutions provided to me, thanks for it: 只是想分享一下:这对我提供的解决方案有用,谢谢你:

format : function(e){
 //function body
 if(e && e.type == 'focus'){
   //execute this condition only on focus event 
 }
}

Just an FYI, we do not explicitly need to pass the event param, it can be as simple as this: 仅仅是一个FYI,我们没有明确需要传递事件参数,它可以像这样简单:

format : function(){
 //function body
 if(e && e.type == 'focus'){
    //execute this condition only on focus event 
 }
}

e will by default have the reference of the event by which it was caused. 默认情况下,e将引用它所引发的事件。 Thanks for all of the input. 感谢所有的输入。

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

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