Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. var voteUp = document.getElementById('vote-up');
  2.  
  3. var handUp = once(function() {
  4. var total = Number(voteUp.innerHTML);
  5. total += 1;
  6. voteUp.innerHTML = total;
  7. });
  8.  
  9. voteUp.addEventListener('click', handUp);
  10.  
  11. function once(fn, context) {
  12. var result;
  13.  
  14. return function() {
  15. if(fn) {
  16. result = fn.apply(context);
  17. fn = null;
  18. saveVote();
  19. }
  20. return result;
  21. };
  22. }
  23.  
  24. function saveVote() {
  25. var votes = voteUp;
  26. var data = Array.prototype.map.call(votes, function(vote){
  27. return[vote];
  28. });
  29. localStorage.setItem('data', JSON.stringify(data));
  30. console.log('saveVote');
  31. }
  32.  
  33.  
  34.  
  35. function loadVote() {
  36. var votes = JSON.parse(localStorage.getItem('data'));
  37. if(!votes){
  38. return;
  39. }
  40. Array.prototype.map.call(votes, function(vote){
  41. return votes;
  42. });
  43. console.log(JSON.parse(localStorage.getItem('votes')));
  44. }
  45.  
  46. loadVote();
  47.  
  48. function once(fn, context) {
  49. var result;
  50.  
  51. return function() {
  52. if(fn) {
  53. result = fn.apply(context || this, arguments);
  54. fn = null;
  55. }
  56.  
  57. return result;
  58. };
  59. }
  60.  
  61. var voteUp = document.getElementById('vote-up');
  62.  
  63. var handUp = once(function() {
  64. var total = Number(voteUp.innerHTML);
  65. total += 1;
  66. voteUp.innerHTML = total;
  67. });
  68.  
  69. voteUp.addEventListener('click', handUp);
  70.  
  71. voteUp.addEventListener('click', function(){
  72. //call your function method to what you want to do with voteUp
  73. //and call this
  74. this.removeEventListener('click');
  75. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement