简体   繁体   English

为什么 setInterval 和 clearInterval 在 JS 中不起作用?

[英]Why setInterval and clearInterval is not working in JS?

I am new to JS.我是 JS 新手。

I have 2 cards like the below,我有两张像下面这样的卡片,

<div class = "card" id ="c1">
     <div class = "content"> </div>
</div>

<div class = "card" id ="c2">
     <div class = "content"> </div>
</div>

The div content in the card 1 should display the dynamic value like Date.now() and the content in card2 should display the static Value.卡片 1 中的 div content应显示类似Date.now()的动态值,卡片 2 中的content应显示静态值。

JS: JS:

function cardData(card) {
    myCard(card);
}

function allCardData() {
    var cards = document.getElementsByClassName("card");
    Array.prototype.forEach.call(cards, (card) => {
      cardData(card);
    });
  }

function mockData(card) {
  
  var setInt;
  // SetInterval function

  function caseOne(card) {
     setInt = setInterval(function() {
      var currentDate = Date.now();
      var val = String(currentDate).substr(8, 2);
    }, 2000);
     return setInt;
   }
   
   //clearInterval
   function caseTwo(card) {
     var res = clearInterval(setInt);
     return res;
   }
   
   
  switch(card) 
    {
     case "c1":
     return {
       target: card,
       value: caseOne(card)
     }
     case "c2":
     return {
       target: card,
       value: caseTwo(card)
     }
     }
     
   function Callback(value, callback) {
    callback(value);
    }
 
  function myCard(card) {
    Callback(mockData(card), function (data) {
      var target = document.getElementById(data.target);
      var content = target.getElementsByClassName('content')[0];
      content.innerHTML = data.value;
    });
  }
 
 
  //Onload function
 window.onload = function () {
 allCardData();
 };

I need a dynamic data every 2S in card1 and a static data Which clears the dynamic value in card2.我需要在card1中每2S一个动态数据和一个static data Which clears the dynamic value card2中static data Which clears the dynamic valuestatic data Which clears the dynamic value

But, it is not working.但是,它不起作用。

Since, callback function expects return , When I pass the function mockData to the callback.因为,回调函数需要return ,当我将函数mockData传递给回调时。 It is not working as expected.它没有按预期工作。

Could someone please help me to start the dynamic data in card1 and clear the data in card2 .?有人可以帮我to start the dynamic data in card1 and clear the data in card2吗?

Many thanks.非常感谢。

This is what the code does:这是代码的作用:

  • Calling allCardData() for window.onloadwindow.onload调用allCardData()
  • allCardData() calls cardData() for each card found with the card's id allCardData()为找到的每张卡片调用cardData()
  • cardData() calls myCard() in an intermediate step cardData()在中间步骤中调用myCard()
  • myCard() makes Callback call mockData() for the card myCard()为卡片进行Callback调用mockData()
  • mockData() calls caseOne() for card c1 and caseTwo() for card c2 mockData()调用caseOne()对卡C1和caseTwo()的卡C2
  • caseOne() sets a 2 second interval caseOne()设置 2 秒间隔
  • caseTwo() cancels the interval caseTwo()取消间隔

There is no time between calling caseOne() and caseTwo() .在调用caseOne()caseTwo()之间没有时间。 They are called after each other with no time in between.他们互相呼唤,中间没有时间。 This means the timer is cancelled immediately after it is initialized.这意味着定时器在初始化后立即被取消。 No time for the 2 second timer to do anything.没有时间让 2 秒计时器做任何事情。

Suggested solution to change caseTwo() to only clear timer after a certain condition has passed.caseTwo()更改为仅在特定条件过去后清除计时器的建议解决方案。 Otherwise the interval will never start.否则间隔将永远不会开始。
You may want to google about async/await , you may need to change your code in other parts:您可能想谷歌一下async/await ,您可能需要更改其他部分的代码:

  async function caseTwo(card) {
    // asynchronously wait, stopping this function until the value is known
    var value = await getValueTwoFromSomewhere();
    
    clearInterval(setInt);
    return value;
  }

Otherwise the code would need to be rewritten to something different.否则,代码将需要重写为不同的东西。
Maybe you want to rethink your solution and post it again as another question.也许您想重新考虑您的解决方案并将其作为另一个问题再次发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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