Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. /**
  2. * Created by foulo on 28/03/2017.
  3. */
  4. $(document).ready(function () {
  5. AddHighScore();
  6. //ClearLocalStorage();
  7. });
  8.  
  9. var arrHighScores = []; // create Global array
  10.  
  11. function AddHighScore() {
  12. // Get the current list
  13. retrieveHighscores();
  14. // Add new entry
  15. //var userName = prompt("userName");
  16. //var difficulty = prompt("difficulty");
  17. //var timePast = prompt("timePast");
  18.  
  19. arrHighScores.push( new objScore("yern", "izan", "999") );
  20. for (i = 0; i < 9; i++) {
  21. var userName = "Nick";
  22. var difficulty = i;
  23. var timePast = i + i + i;
  24. arrHighScores.push( new objScore(userName, difficulty, timePast) );
  25. }
  26.  
  27.  
  28. // Only store the top scores & sorting
  29. arrHighScores.sort(function(a,b){return a.timePast-b.timePast});
  30. // Save it
  31. SetHighScores();
  32. // Print out the new High Score list
  33. ShowHighScoreValues();
  34. }
  35.  
  36. var retrieveHighscores = function () {
  37. var RawData = localStorage.HighScores;
  38.  
  39. if (RawData != null) {
  40. var HighScoresStored = JSON.parse(RawData);
  41. arrHighScores = []; // reset
  42. $.each(HighScoresStored, function(i, obj) {
  43. arrHighScores.push( new objScore(obj.userName, obj.difficulty ,obj.timePast) );
  44. });
  45. }else{
  46. console.log("geen localstorage gevonden");
  47. };
  48. }
  49.  
  50. function objScore(userName, difficulty, timePast){
  51. this.userName = userName;
  52. this.difficulty = difficulty;
  53. this.timePast = timePast;
  54. };
  55.  
  56. function SetHighScores() {
  57. var myJsonString = JSON.stringify(arrHighScores);
  58. localStorage.HighScores = myJsonString;
  59. }
  60.  
  61. function ShowHighScoreValues() {
  62. $.each(arrHighScores, function(i, obj) {
  63. $("#mainHighscore table").append(
  64. "<tr>" +
  65. "<td>" +
  66. (i + 1) +
  67. "</td>" +
  68. "<td>" +
  69. obj.userName +
  70. "</td>" +
  71. "<td>" +
  72. obj.difficulty +
  73. "</td>" +
  74. "<td>" +
  75. obj.timePast
  76. +
  77. "</td>" +
  78. "</tr>"
  79. );
  80. return i<9;
  81. });
  82. }
  83.  
  84. function ClearLocalStorage() {
  85. localStorage.clear();
  86. arrHighScores = []; // reset incase the page isn't refreshed
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement