0

I am begginer in programming. I have made these stopwatch for speedsolves using HTML and JavaScript.

Is there any way of saving my average solve time using Javascript only?

For instance i have some variable with loop:

var time=0;
if (true){
  time++;
}

and after I close the HTML site, close the browser and turn off the computer, after opening the site again I want to get var time=value before closing the site

I'm familiar with PHP and databases, but I dont really want to use it, since I have to start a server.

  • 1
    Possible duplicate of [Is it possible to write data to file using only JavaScript?](https://stackoverflow.com/questions/21012580/is-it-possible-to-write-data-to-file-using-only-javascript) – showdev Oct 25 '19 at 19:19

3 Answers3

0

Little more information will help, but, as you are talking of storage, you can use localstorage or sessionstorage within javascript to store the values.

  • I have used localstorage like you all recommended me to and it did the trick. You were right about the lack of information, so I have edited the question a little. Thank you :) – Teemo is Ready Oct 26 '19 at 11:09
0

For saving files on the local device, you can use FileSaver. But in your situation, an easier solution would be to take advantage of localstorage.

Note: From what I've found, LocalStorage won't work without a server. So if you want to store data and aren't using a server, FileSaver may work better. Then again, I don't know if it's possible to load the data in a file saved in FileSaver into javascript in a browser, especially if you don't have a server

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
0

If you just want to save data for your program to read later, you can use localStorage or sessionStorage. That is, localStorage.setItem('name', 'value') and localStorage.getItem('name') to read it. (They're the same, except sessionStorage gets cleared once the browser is closed, while localStorage doesn't.)

To actually save a file, you can do that only by triggering it to be downloaded. First you create a Blob with the content you want for the file, then you get an object URL for that Blob. Assign the URL to the href of a link with a "download" attribute and click it.

function saveFile(contents, name) {
  const blob = new Blob([contents], {type: 'text/plain'});
  const url = URL.createObjectURL(blob);
  const link = document.createElement('a');
  link.download = name;
  link.href = url;
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
}
IceMetalPunk
  • 5,476
  • 3
  • 19
  • 26