簡體   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