Search

Tutorials, tips&tricks, snippets..

#KISS: KEEP IT SIMPLE, STUPID!

Category

Client side

Disable onClick after first click to avoid double (or more) clicks

In my app (but this is valid also for web app), I have a problem: if user clicks twice (or more), twice requests (or more) are sent to the API server. Here a snippet to avoid this behavior with useless requests.

var pending = false;

function Foo(param)
{
    // test to see if something else set the state to pending.
    // if so... return, we don't want this to happen.
    if(pending) 
        return;

    pending = true; // raise the flag!

    /* ... later ... */
    // in the success method of your AJAX call add:
    pending = false;
}
Advertisements

JavaScript: add month to Date object

Here two simple snippets useful to add (or subtract) months to a Date object:
 

function add_months(dt, n) 
{
    return new Date(dt.setMonth(dt.getMonth() + n));      
}

dt = new Date();
console.log(add_months(dt, 10).toString());  

You can also create it as prototype, if you prefer:

Date.prototype.add_months = function(w){

var date = this;
date.setMonth( date.getMonth()+10 );

return date;

};

var date = new Date();
console.log( date.add_months(10).toString() );

Powered by WordPress.com.

Up ↑