Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140734

JQuery - Cancel event asynchronously

$
0
0

I'm trying to cancel an event asynchronously using jQuery, is it possible?

I always cancel events using the sync form returning false. How would be it in a callback for example?

Note: I don't want return the callback, I want to cancel the event using something like event.cancelEvent if it is possible. Thank you!

<button id="sync-button">Sync Click me!</button>
<button id="async-button">Async Click Me</button>
<button id="reset">Reset</button>
<button id="supress">Supress event</button>

<p id="display"></p>    
var supressEvent = false;

function buttonAction(){
    $('#display').append('<p>Bang!</p>');
}

function asyncTask(event, callback){

    console.log('Do some async task...');

    setTimeout(function(){
        console.log('DONE!');
        callback();
    }, 1000);            
}

$("#reset").click(function(){
    $('#display').text('');
    $('#supress').text('Supress event');

    supressEvent = false;
});

$('#supress').click(function(){

    if(supressEvent){
        $(this).text('Supress event');
    }
    else{          
        $(this).text('Unsupress event');
    }

    supressEvent = !supressEvent;
});        

$('#sync-button').click(function(event){

    if(supressEvent){
        return false;
    }

    buttonAction();

});

$('#async-button').click(function(event){

    asyncTask(event, function(){

        if(supressEvent){
            //TODO: logic to cancel the event
        }

        buttonAction();
    });

    //more statements here
});   


Viewing all articles
Browse latest Browse all 140734

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>