Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by foulo on 28/03/2017.
- */
- $(document).ready(function () {
- AddHighScore();
- //ClearLocalStorage();
- });
- var arrHighScores = []; // create Global array
- function AddHighScore() {
- // Get the current list
- retrieveHighscores();
- // Add new entry
- //var userName = prompt("userName");
- //var difficulty = prompt("difficulty");
- //var timePast = prompt("timePast");
- arrHighScores.push( new objScore("yern", "izan", "999") );
- for (i = 0; i < 9; i++) {
- var userName = "Nick";
- var difficulty = i;
- var timePast = i + i + i;
- arrHighScores.push( new objScore(userName, difficulty, timePast) );
- }
- // Only store the top scores & sorting
- arrHighScores.sort(function(a,b){return a.timePast-b.timePast});
- // Save it
- SetHighScores();
- // Print out the new High Score list
- ShowHighScoreValues();
- }
- var retrieveHighscores = function () {
- var RawData = localStorage.HighScores;
- if (RawData != null) {
- var HighScoresStored = JSON.parse(RawData);
- arrHighScores = []; // reset
- $.each(HighScoresStored, function(i, obj) {
- arrHighScores.push( new objScore(obj.userName, obj.difficulty ,obj.timePast) );
- });
- }else{
- console.log("geen localstorage gevonden");
- };
- }
- function objScore(userName, difficulty, timePast){
- this.userName = userName;
- this.difficulty = difficulty;
- this.timePast = timePast;
- };
- function SetHighScores() {
- var myJsonString = JSON.stringify(arrHighScores);
- localStorage.HighScores = myJsonString;
- }
- function ShowHighScoreValues() {
- $.each(arrHighScores, function(i, obj) {
- $("#mainHighscore table").append(
- "<tr>" +
- "<td>" +
- (i + 1) +
- "</td>" +
- "<td>" +
- obj.userName +
- "</td>" +
- "<td>" +
- obj.difficulty +
- "</td>" +
- "<td>" +
- obj.timePast
- +
- "</td>" +
- "</tr>"
- );
- return i<9;
- });
- }
- function ClearLocalStorage() {
- localStorage.clear();
- arrHighScores = []; // reset incase the page isn't refreshed
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement