热门关键字:
jquery > jquery教程 > html5 > html5本地存储实现游戏历史记录

html5本地存储实现游戏历史记录

2046
作者:管理员
发布时间:2015/5/8 10:09:51
评论数:0
转载请自觉注明原文:http://www.jq-school.com/Show.aspx?id=553

html5本地存储实现游戏历史记录

//获取游戏历史记录
function gameHistory(){
	//读取 localStage 本地存储中的浏览记录
	if (window.localStorage.games){
		var jsonStr = window.localStorage.games;
		if(jsonStr.length<=0 || jsonStr=="" || jsonStr == null){
			$("#recent_title").hide();
			$("#recent").hide();
		}else{
			var data = JSON.parse("["+jsonStr+"]");
			var len = data.length;
			if (len > 0){
				for(var i=0; i< len; i++){
					$("#recent").append('<a style="width:20%;" href="toGameRanking.do?id='+data[i].GAME_ID+'"><img src="'+data[i].GAME_LOGO+'" /><br />'+data[i].GAME_NAME+'</a>');
					if (i==4){
						break;
					}
				}
			}else{
				$("#recent_title").hide();
				$("#recent").hide();
			}
		}
	}else{
		$("#recent_title").hide();
		$("#recent").hide();
	}
}

//设置游戏浏览记录到HTML5本地存储
function setHistory(gameId,game){
	//格式为:game = '{"GAME_ID":"1","GAME_NAME":"2048","GAME_LOGO":"img/e168eff1-4c90-489a-aaa9-3d9eea8350e9.png"}';
	if(game.length>0){
		if (window.localStorage.games){
			if (window.localStorage.games.indexOf(gameId)<0){
				if(window.localStorage.games.length<=0){
					window.localStorage.games = game;
				}else{
					window.localStorage.games = game+","+window.localStorage.games;
				}
			}
		}else{
			window.localStorage.games=game;
		}
	}
}





如果您觉得本文的内容对您的学习有所帮助:支付鼓励



关键字:html5 html5本地存储
友荐云推荐