在本地存储中持续检查一个函数是否返回true。

huangapple go评论96阅读模式
英文:

Keep checking if a function returns true in local storage

问题

I'm trying to check if a function returns true in an if/else statement, however I want to keep checking if the function returns true and when it does to execute the code...

我试图在if/else语句中检查函数是否返回true,但我想要持续检查函数是否返回true,当它返回true时执行代码...

How would I go about this? I have tried setInterval and setTimeout and it won't repeat, unless I'm missing something...

我应该如何做到这一点?我尝试过setIntervalsetTimeout,但它不会重复,除非我漏掉了什么...

I have also tried a while loop but this is inefficient and crashes the browser...

我还尝试过使用while循环,但这是低效的,并且会导致浏览器崩溃...

However if I use an setInterval() how would I clear it once x returns true?

但如果我使用setInterval(),一旦x返回true,我该如何清除它?

Here is a snippet of my code for an example.

以下是我的示例代码片段。

  1. function x() {
  2. if (localStorage.getItem("a") === null) {
  3. return false;
  4. } else {
  5. return true;
  6. }
  7. }
  8. if (!x()) {
  9. console.log("Returned True!");
  10. update();
  11. } else {
  12. console.log("False");
  13. }
英文:

I'm trying to check if a function returns true in an if/else statement, however I want to keep checking if the function returns true and when it does to execute the code...

How would I go about this? I have tried setInterval and setTimeout and it won't repeat, unless I'm missing something...

I have also tried a while loop but this is inefficient and crashes the browser...

However if I use an setInterval() how would I clear it once x returns true?

Here is a snippet of my code for an example.

  1. function x() {
  2. if (localStorage.getItem("a") === null) {
  3. return false;
  4. } else {
  5. return true;
  6. }
  7. }
  8. if (!x()) {
  9. console.log("Returned True!");
  10. update();
  11. } else {
  12. console.log("False");
  13. }

答案1

得分: 1

setInterval() 应该可以工作。

  1. function x() {
  2. return localStorage.getItem("a") !== null;
  3. }
  4. let interval = setInterval(() => {
  5. if (x()) {
  6. console.log("Returned True!");
  7. update();
  8. clearInterval(interval);
  9. } else {
  10. console.log("False");
  11. }
  12. }, 500);
英文:

setInterval() should work

  1. function x() {
  2. return localStorage.getItem("a") !== null;
  3. }
  4. let interval = setInterval(() => {
  5. if (x()) {
  6. console.log("Returned True!");
  7. update();
  8. clearInterval(interval);
  9. } else {
  10. console.log("False");
  11. }
  12. }, 500);

答案2

得分: 0

function x() {
if (localStorage.getItem("a") === null) {
return false;
} else {
return true;
}
}

setInterval(x(), 1000);
setInterval(x, 1000);

第一个将在第二个清除之前运行一次。

英文:

Are you calling your function in the interval like x() or x?

  1. function x() {
  2. if (localStorage.getItem("a") === null) {
  3. return false;
  4. } else {
  5. return true;
  6. }
  7. }
  8. setInterval(x(), 1000);
  9. setInterval(x, 1000);

The first will run once the second till you clear it

huangapple
  • 本文由 发表于 2023年4月20日 07:40:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76059545.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定