简体   繁体   English

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

[英]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.

相关问题 我想在任何 javascript 或 servlet 的帮助下使用 html 文本框从文本文件中读取和替换字符串? - I want to read and replace string from text file using html textboxes with the help of any javascript or servlet? 我想在HTML文本框中的JavaScript文件中写入数据流 - I want to write a stream of data that are in a JavaScript file in an HTML textbox 我正在创建一个Mac应用程序,该应用程序需要读取和写入HTML文件。 我该如何实现? - I'm creating a Mac application that needs to read and write to/from an HTML file. How do I achieve this? 我想在android中的TextView上显示html文件的内容(带格式) - I want to display the contents of html file (with formatting) on TextView in android 如何从一个 HTML 文件中读取数据并使用 JavaScript 将其显示在另一个文件中 - How do I read data from one HTML file and display it in another with JavaScript 如何读取和显示 html 文件 - How to read and display html file 读取 excel 文件并显示在 html 中 - Read excel file and display in html Javascript从文本文件中读取值并显示在html页面中 - Javascript to read value from a text file and display in html page 如何从 csv 文件读取和显示数据到 html 网页? - How to read and display data from a csv file to html webpage? 如何在网页上显示功能的结果。 我想显示平均分数和字母等级。 我已经附上了我的代码。 - How do I display my result from a function on the web page. I want to display the average score and the letter grade. I have enclosed my code.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM