简体   繁体   中英

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. Is there any good way to transfer javascript variable into file from which i can then read? Or directly to another html would be best if possible. 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.

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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