LocalStorage没有传递数字9

时间:2019-05-16 20:16:37

标签: javascript local-storage

我遇到localStorage的问题,我的localStorage没有传递数字9。如何解决此问题?

我尝试了很多事情,但是没有用。

我正在这样做:

if (typeof(Storage) !== "undefined") {
  if (sessionStorage.pontos) {
    sessionStorage.pontos = Number(sessionStorage.pontos) + 1;
  } else {
    sessionStorage.pontos = 1;
  }

  document.getElementById('pontos').innerHTML = "PONTOS: " + sessionStorage.pontos;
} else {
  document.getElementById('pontos').innerHTML = "ERROR";
}
if (typeof(Storage) !== "undefined") {
  if (sessionStorage.pontos > localStorage.Recpontos) {
    localStorage.Recpontos = Number(localStorage.Recpontos) + 1;
  }
}

单击按钮时会发生此操作。 我转到浏览器控制台,什么也没有,在控制台中显示以下内容:

> localStorage.Recpontos
 "9"

> sessionStorage.pontos
 "10"

这不是应该发生的,因为当sessionStorage.pontos大于localStorage.Recpontos时,这两个值必须相同。 9只有一个地方,10只有两个地方,我认为这就是问题所在。

我希望localStorage.Recpontos大于sessionStorage.point时将{{1}}加1。

1 个答案:

答案 0 :(得分:1)

您需要将值比较为数字,而不是字符串。

const stream = require("stream");
const fs = require("fs");
const { readdir, lstat } = fs.promises;
const path = require("path");

class Walk extends stream.Readable {
  constructor(root, maxDepth = Infinity) {
    super();

    this._maxDepth = maxDepth;

    // These fields allow us to remember where we were when we have to pause our
    // work.

    // The path of the directory to process when we resume processing, and the
    // depth of this directory.
    this._curdir = [root, 1];

    // The directories still to process.
    this._dirs = [this._curdir];

    // The list of files to process when we resume processing.
    this._files = [];

    // The location in `this._files` were to continue processing when we resume.
    this._ix = 0;

    // A flag recording whether or not the fetching of files is currently going
    // on.
    this._started = false;
  }

  async _fetch() {
    // Recall where we were by loading the state in local variables.
    let files = this._files;
    let dirs = this._dirs;
    let [dir, depth] = this._curdir;
    let ix = this._ix;

    while (true) {
      // If we've gone past the end of the files we were processing, then
      // just forget about them. This simplifies the code that follows a bit.
      if (ix >= files.length) {
        ix = 0;
        files = [];
      }

      // Read directories until we have files to process.
      while (!files.length) {
        // We've read everything, end the stream.
        if (dirs.length === 0) {
          // This is how the stream API requires us to indicate the stream has
          // ended.
          this.push(null);

          // We're no longer running.
          this._started = false;
          return;
        }

        // Here, we get the next directory to process and get the list of
        // files in it.
        [dir, depth] = dirs.pop();

        try {
          files = await readdir(dir, { withFileTypes: true });
        }
        catch (ex) {
          // This is a proof-of-concept. In a real application, you should
          // determine what exceptions you want to ignore (e.g. EPERM).
        }
      }

      // Process each file.
      for (; ix < files.length; ++ix) {
        const dirent = files[ix];
        // Don't include in the results those files that are not directories,
        // files or symbolic links.
        if (!(dirent.isFile() || dirent.isDirectory() || dirent.isSymbolicLink())) {
          continue;
        }

        const fullPath = path.join(dir, dirent.name);
        if (dirent.isDirectory() & depth < this._maxDepth) {
          // Keep track that we need to walk this directory.
          dirs.push([fullPath, depth + 1]);
        }

        // Finally, we can put the data into the stream!
        if (!this.push(`${fullPath}\n`)) {
          // If the push returned false, we have to stop pushing results to the
          // stream until _read is called again, so we have to stop.

          // Uncomment this if you want to see when the stream stops.
          // console.log("STOP");

          // Record where we were in our processing.
          this._files = files;
          // The element at ix *has* been processed, so ix + 1.
          this._ix = ix + 1;
          this._curdir = [dir, depth];

          // We're stopping, so indicate that!
          this._started = false;
          return;
        }
      }
    }
  }

  async _read() {
    // Do not start the process that puts data on the stream over and over
    // again.
    if (this._started) {
      return;
    }

    this._started = true; // Yep, we've started.

    // Uncomment this if you want to see when the stream starts.
    // console.log("START");

    await this._fetch();
  }
}

// Change the paths to something that makes sense for you.
stream.pipeline(new Walk("/home/", 5),
                fs.createWriteStream("/tmp/paths3.txt"),
                (err) => console.log("ended with", err));

如果将它们作为字符串进行比较,则会执行字典比较,并且会walkdir,因为 if (Number(sessionStorage.pontos) > Number(localStorage.Recpontos)) { localStorage.Recpontos = Number(localStorage.Recpontos) + 1; }