Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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();
- 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);
- voteUp.addEventListener('click', function(){
- //call your function method to what you want to do with voteUp
- //and call this
- this.removeEventListener('click');
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement