Search code examples
javascriptjquerymouseeventjquery-events

How to temporary disable the click function when it is executing?


$("#prevPage").live("click",function(e) {
.................
});

For example, when the user have already clicked on the prevPage, the statement inside it is running, if the user click on it instantly , it will trigger again. However, I would like the click event trigger only after all the statement inside it have finish execution, How to achieve that?


Solution

  • How about this or something similar:

    <script type="text/javascript">
        // disable command while function is being executed.
        var sample = { 
            isExecuting : 0, 
            doWork : function (e) { 
                if (sample.isExecuting === 1) return;
                sample.isExecuting = 1;
                // do work -- whatever you please
                sample.isExecuting = 0; // say: I'm done!
            }
        };
        // live or bind
        $("#prevPage").bind("click",function(e) {
             sample.doWork(e);
        });
    </script>
    

    simple 'shield' to block a multiple-call scenario.