Dom Adams
Dom Adams

Reputation: 281

JavaScript OnClick function do not call after its been called once

i have a function which is called on a onclick. Now it has to be on a onclick as thats when the data is being brought in.. i want to know how i can get it to disable and not be called after its been called once.

function Compare(){
    var OldVariants = Variants();
    var RequestVariants = ProductRequests();

    RequestVariants.forEach(function(req) {
        OldVariants.some(function(variant) {
            if (variant.Id == req.Id) {
                variant.Quantity(req.Quantity);
                return true; // Stops the inner loop
            }
        });
    });
}

Upvotes: 0

Views: 209

Answers (2)

Sandeep Choudhary
Sandeep Choudhary

Reputation: 260

In your function, unbind the click event. Ex:

button.onClick = function({ 
    button.unbind('click');
}

Upvotes: 0

kapantzak
kapantzak

Reputation: 11750

You can achieve with jQuery one() like so:

$(document).one('click', '#element', function() {
    // your code..
});

Now, your function will fire only for the first click on #element

Upvotes: 2

Related Questions