[英]I want to write and read from file in order to display score in different html
I want to create snake game, everything works well, but I wish to pass score to another html file and I am unable. 我想创建蛇游戏,一切运作良好,但我希望将分数传递到另一个html文件,我无法。 Is there any good way to transfer javascript variable into file from which i can then read? 有没有什么好方法可以将javascript变量传输到我可以读取的文件中? Or directly to another html would be best if possible. 或者直接到另一个html,如果可能的话最好。 Thanks for every answer! 谢谢你的每一个答案! I also wish to wait a little so person can see where he died but command "wait(function, miliseconds)" did not work. 我也希望稍微等一下,这样一个人可以看到他去世的地方,但命令“等待(功能,毫秒)”不起作用。
I´ve already tryed this piece of code which resulted into nothing. 我已经尝试过这段代码而没有任何结果。
set fso = CreateObject("Scripting.FileSystemObject");
set s = fso.CreateTextFile("../pages/score.txt", True);
s.writeline(score);
s.Close();
My engame function right now: 我现在的功能:
function endGame()
{
clearInterval(game);
clearInterval(spawnEnemy);
clearInterval(spawnBonus);
dead.play();
window.location.href = "../pages/score.html";
}
As wrote above I need to solve issue with file or displaying score in different html and also to wait before going into another page via window.location.href command. 如上所述,我需要解决文件问题或在不同的html中显示得分,并在通过window.location.href命令进入另一个页面之前等待。
You can store variables in sessionStorage (is stored until the user closes the browser tab) or in localStorage (is stored without expiration date) and can access them from different sites then. 您可以在sessionStorage中存储变量(存储直到用户关闭浏览器选项卡)或localStorage存储(存储没有过期日期),然后可以从不同的站点访问它们。
This is how you can store a value: 这是您存储值的方法:
// store the score for future use
localStorage.setItem("score", 7);
This is how you can retrieve it: 这是你如何检索它:
// display the stored score
console.log("The stored score is: " + localStorage.getItem("score"));
You can trigger a function after a wait period like so: 你可以在等待一段时间后触发一个函数,如下所示:
//change this to the number of seconds you want to wait
var nrOfSeconds = 5;
//use the function you want to trigger after the wait period here
function theFunctionYouWantToTrigger(){
console.log("Function was triggered after " + nrOfSeconds + " seconds");
}
//since the timeout needs the time in milliseconds we need to mulitply the seconds with 1000
window.setTimeout(theFunctionYouWantToTrigger, nrOfSeconds*1000);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.