-1

I have a vote up +1 one time by user, now I'm watching. I need loadVote when refresh page, because saveVote is running ok, I think of my solution is in return votes; of the loadVote function. And Where is ok the saveVote();, in the function once or after of return votes;

var voteUp = document.getElementById('vote-up');

var handUp = once(function() {
    var total = Number(voteUp.innerHTML);
    total += 1;
    voteUp.innerHTML = total;
});

voteUp.addEventListener('click', handUp);

function once(fn, context) {
    var result;

        return function() {
            if(fn) {
                result = fn.apply(context);
                fn = null;
                    saveVote();
            }
            return result;
        };
}

function saveVote() {
    var votes = voteUp;
    var data = Array.prototype.map.call(votes, function(vote){
        return[vote];
    });
        localStorage.setItem('data', JSON.stringify(data));
            console.log('saveVote');
}



function loadVote() {
    var votes = JSON.parse(localStorage.getItem('data'));
        if(!votes){
            return;
        }
            Array.prototype.map.call(votes, function(vote){
                return votes;
            });
        console.log(JSON.parse(localStorage.getItem('votes')));
}

loadVote();
supportsp
  • 63
  • 2
  • 11
  • You generally solve that by recording the IP serverside, otherwise just reloading the page lets someone vote multiple times. If that's not an issue, jQuery has `one()` ! – adeneo Dec 13 '14 at 17:22
  • possible duplicate of [Vote only once (JavaScript)](http://stackoverflow.com/questions/27460685/vote-only-once-javascript) – CD.. Dec 13 '14 at 17:23
  • http://www.9lessons.info/2009/08/vote-with-jquery-ajax-and-php.html – adeneo Dec 13 '14 at 17:24
  • http://api.jquery.com/off/ – Pratik Dec 13 '14 at 17:25
  • @adeneo I will use localStorage to save and load dates with JSON, but now, "I need vote-up once". Because now I can vote-up infinitely by every click that I do. – supportsp Dec 13 '14 at 17:27
  • You can just disable the button on the first click (it means, inside the `handUp` function). But, note, all these solutions you just avoid the user of votes several times by clicking on the button many times... Because the user can just reload the page and vote again. – Hilder Vitor Lima Pereira Dec 13 '14 at 17:27
  • Even with localStorage, it's trivial to open the console and delete it, and vote as many times as one wants. – adeneo Dec 13 '14 at 17:30

2 Answers2

2

love David Walsh's post on this HERE

DEMO

function once(fn, context) { 
    var result;

    return function() { 
        if(fn) {
            result = fn.apply(context || this, arguments);
            fn = null;
        }

        return result;
    };
}

var voteUp = document.getElementById('vote-up');

var handUp = once(function() {
    var total = Number(voteUp.innerHTML);
    total += 1;
    voteUp.innerHTML = total;
});

voteUp.addEventListener('click', handUp);
Community
  • 1
  • 1
Todd
  • 5,314
  • 3
  • 28
  • 45
2

Simply do this:

voteUp.addEventListener('click', function(){
  //call your function method to what you want to do with voteUp
  //and call this
   this.removeEventListener('click');
});
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231