繁体   English   中英

我想写和从文件中读取,以便在不同的HTML中显示分数

[英]I want to write and read from file in order to display score in different html

我想创建蛇游戏,一切运作良好,但我希望将分数传递到另一个html文件,我无法。 有没有什么好方法可以将javascript变量传输到我可以读取的文件中? 或者直接到另一个html,如果可能的话最好。 谢谢你的每一个答案! 我也希望稍微等一下,这样一个人可以看到他去世的地方,但命令“等待(功能,毫秒)”不起作用。

我已经尝试过这段代码而没有任何结果。

set fso = CreateObject("Scripting.FileSystemObject"); 
        set s  = fso.CreateTextFile("../pages/score.txt", True);
        s.writeline(score);
        s.Close();

我现在的功能:

function endGame() 
    {   
        clearInterval(game);
        clearInterval(spawnEnemy);
        clearInterval(spawnBonus);
        dead.play();
        window.location.href = "../pages/score.html";
    }

如上所述,我需要解决文件问题或在不同的html中显示得分,并在通过window.location.href命令进入另一个页面之前等待。

您可以在sessionStorage中存储变量(存储直到用户关闭浏览器选项卡)或localStorage存储(存储没有过期日期),然后可以从不同的站点访问它们。

这是您存储值的方法:

// store the score for future use
localStorage.setItem("score", 7);

这是你如何检索它:

// display the stored score
console.log("The stored score is: " + localStorage.getItem("score"));

你可以在等待一段时间后触发一个函数,如下所示:

//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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM