bind event only once

前端 未结 13 2306
予麋鹿
予麋鹿 2020-12-05 03:45

I have the following code:

function someMethod()
{
  $(obj).click(function {});
}

someMethod is called twice and thus click event is binded

13条回答
  •  没有蜡笔的小新
    2020-12-05 04:16

    var bound = false;
    
    function someMethod()
    {
        if(!bound)
        {
           $(obj).click(function {});
           bound = true;
        }
    }
    

    but I would probably look into why it;s being called twice before making some kind of workaround.

提交回复
热议问题