execute a function only once

后端 未结 3 973
南旧
南旧 2020-12-22 04:14

I have a function which executes on click event, but the thing is that I want it to execute only once.

The function that get\'s executed on click represents a googl

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-22 04:57

    you could bind click only once like:

    $(document).one('click', function(e) {
        //do your code here
    });
    

    OR

    $('#someBtn').click(function(){    
        if ($(this).attr('data-once')!='already_clicked' ){
            //your code here
            $(this).attr('data-once', 'already_clicked');
        }
    });
    

提交回复
热议问题