如何在我的标签更改时每次将计数器重置为1?

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

How can I reset the counter to 1 each time my labels changes?

问题

我有这段代码,它应该作为一个呼吸锻炼计数,如下所示:

8秒吸气
8秒 "保持吸气"
12秒 "呼气"
8秒 "保持呼气"

一切都正常工作,除了计数器会不断增加而不重置为1。它应该是,例如从1到8,然后再从1到8,然后从1到12,依此类推。

英文:

I have this code that is supposed act as a breathing exercise counting as follow:

8 second inhale 
8 second "hold inhale"
12 second "exhale"
8 second "hold exhale" 

everything is working fine except the counter will increment without resetting to 1. it should be, e. g. 1 to 8 and then 1 to 8 again and then 1 to 12 and so on.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// Function to start the timer
function startTimer() {
  var timer = document.getElementById(&#39;timer&#39;);
  var label = document.getElementById(&#39;label&#39;);
  var count = 1;

  // Interval for the timer
  var interval = setInterval(function() {
    // Update the timer display and label
    timer.textContent = count;

    if (count &lt;= 8) {
      label.textContent = &#39;Inhale&#39;;
    } else if (count &lt;= 16) {
      label.textContent = &#39;Pause Inhale&#39;;
    } else if (count &lt;= 28) {
      label.textContent = &#39;Exhale&#39;;
    } else if (count &lt;= 36) {
      label.textContent = &#39;Pause Exhale&#39;;
    }

    // Increment count
    count++;

    // Stop the timer after reaching 8, 8, 12, 8 pattern
    if (count === 45) {
      clearInterval(interval);
      startTimer();
    }
  }, 1000); // 1000 milliseconds = 1 second
}

// Start the timer initially
startTimer();

<!-- language: lang-css -->

body {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
  margin: 0;
  font-family: Arial, sans-serif;
  text-align: center;
}

h1 {
  font-size: 24px;
  font-weight: bold;
}

p {
  font-size: 18px;
}

<!-- language: lang-html -->

&lt;h1&gt;Special Timer&lt;/h1&gt;
&lt;p id=&quot;timer&quot;&gt;&lt;/p&gt;
&lt;p id=&quot;label&quot;&gt;&lt;/p&gt;

<!-- end snippet -->

答案1

得分: 0

你应该添加另一个变量来保存每个步骤的计数,就像这样。

// 启动计时器的函数
function startTimer() {
  var timer = document.getElementById('timer');
  var label = document.getElementById('label');
  var count = 0;
  // 用于保存每个步骤计数的变量 'stepCount'
  var stepCount = 0;

  // 计时器的时间间隔
  var interval = setInterval(function() {
    // 更新计时器显示和标签

    if (count === 0) {
      label.textContent = '吸气';
    } else if (count === 8) {
      // 重置 stepCount 为 0
      stepCount = 0;
      label.textContent = '暂停吸气';
    } else if (count === 16) {
      // 重置 stepCount 为 0
      stepCount = 0;
      label.textContent = '呼气';
    } else if (count === 28) {
      // 重置 stepCount 为 0
      stepCount = 0;
      label.textContent = '暂停呼气';
    }

    // 增加计数
    count++;
    // 增加 stepCount
    stepCount++;
    // 在这里显示计数器
    timer.textContent = stepCount;

    // 当计数达到 8、8、12、8 的模式时停止计时器
    if (count === 36) {
      clearInterval(interval);
      startTimer();
    }
  }, 1000); // 1000 毫秒 = 1 秒
}

// 初始时启动计时器
startTimer();
body {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
  margin: 0;
  font-family: Arial, sans-serif;
  text-align: center;
}

h1 {
  font-size: 24px;
  font-weight: bold;
}

p {
  font-size: 18px;
}
<h1>特殊计时器</h1>
<p id="timer"></p>
<p id="label"></p>
英文:

You should add another variable to hold each step counter, like this.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// Function to start the timer
function startTimer() {
  var timer = document.getElementById(&#39;timer&#39;);
  var label = document.getElementById(&#39;label&#39;);
  var count = 0;
  // Variable &#39;stepCount&#39; to hold each step counter
  var stepCount = 0;

  // Interval for the timer
  var interval = setInterval(function() {
    // Update the timer display and label


    if (count === 0) {
      label.textContent = &#39;Inhale&#39;;
    } else if (count === 8) {
	  // reset stepCount to 0
	  stepCount = 0;
      label.textContent = &#39;Pause Inhale&#39;;
    } else if (count === 16) {
	  // reset stepCount to 0
	  stepCount = 0;
      label.textContent = &#39;Exhale&#39;;
    } else if (count === 28) {
	  // reset stepCount to 0
	  stepCount = 0;
      label.textContent = &#39;Pause Exhale&#39;;
    }
	
    // Increment count
    count++;
	// Increment stepCount
	stepCount++;
	//You show here the counter
    timer.textContent = stepCount;

    // Stop the timer after reaching 8, 8, 12, 8 pattern
    if (count === 36) {
      clearInterval(interval);
      startTimer();
    }
  }, 1000); // 1000 milliseconds = 1 second
}

// Start the timer initially
startTimer();

<!-- language: lang-css -->

body {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
  margin: 0;
  font-family: Arial, sans-serif;
  text-align: center;
}

h1 {
  font-size: 24px;
  font-weight: bold;
}

p {
  font-size: 18px;
}

<!-- language: lang-html -->

&lt;h1&gt;Special Timer&lt;/h1&gt;
&lt;p id=&quot;timer&quot;&gt;&lt;/p&gt;
&lt;p id=&quot;label&quot;&gt;&lt;/p&gt;

<!-- end snippet -->

答案2

得分: 0

使用不同的变量来表示当前动作的计数和整个周期的长度。

// Function to start the timer
function startTimer() {
  var timer = document.getElementById('timer');
  var label = document.getElementById('label');
  label.textContent = '吸气';
  var count = 1; // 当前动作计数
  var segcount = 1; // 整个周期的长度计数

  // 定时器间隔
  var interval = setInterval(function() {
    // 更新计时器显示和标签
    timer.textContent = segcount;

    if (count == 8) {
      label.textContent = '暂停吸气';
      segcount = 0;
    } else if (count == 16) {
      label.textContent = '呼气';
      segcount = 0;
    } else if (count == 28) {
      label.textContent = '暂停呼气';
      segcount = 0;
    }

    // 增加计数
    count++;
    segcount++;

    // 在达到8、8、12、8的模式后停止计时器
    if (count === 45) {
      clearInterval(interval);
      startTimer();
    }
  }, 1000); // 1000毫秒 = 1秒
}

// 初始启动计时器
startTimer();
body {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
  margin: 0;
  font-family: Arial, sans-serif;
  text-align: center;
}

h1 {
  font-size: 24px;
  font-weight: bold;
}

p {
  font-size: 18px;
}
<h1>特殊计时器</h1>
<p id="timer"></p>
<p id="label"></p>
英文:

Use different variables for the count of the current action and the length of the entire cycle.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// Function to start the timer
function startTimer() {
  var timer = document.getElementById(&#39;timer&#39;);
  var label = document.getElementById(&#39;label&#39;);
  label.textContent = &#39;Inhale&#39;;
  var count = 1;
  var segcount = 1;

  // Interval for the timer
  var interval = setInterval(function() {
    // Update the timer display and label
    timer.textContent = segcount;

    if (count == 8) {
      label.textContent = &#39;Pause Inhale&#39;;
      segcount = 0;
    } else if (count == 16) {
      label.textContent = &#39;Exhale&#39;;
      segcount = 0;
    } else if (count == 28) {
      label.textContent = &#39;Pause Exhale&#39;;
      segcount = 0;
    }

    // Increment count
    count++;
    segcount++;

    // Stop the timer after reaching 8, 8, 12, 8 pattern
    if (count === 45) {
      clearInterval(interval);
      startTimer();
    }
  }, 1000); // 1000 milliseconds = 1 second
}

// Start the timer initially
startTimer();

<!-- language: lang-css -->

body {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
  margin: 0;
  font-family: Arial, sans-serif;
  text-align: center;
}

h1 {
  font-size: 24px;
  font-weight: bold;
}

p {
  font-size: 18px;
}

<!-- language: lang-html -->

&lt;h1&gt;Special Timer&lt;/h1&gt;
&lt;p id=&quot;timer&quot;&gt;&lt;/p&gt;
&lt;p id=&quot;label&quot;&gt;&lt;/p&gt;

<!-- end snippet -->

答案3

得分: 0

你可以使用有限状态机(https://en.wikipedia.org/wiki/Finite-state_machine):

// Function to start the timer
function startTimer() {

  const timer = document.getElementById('timer');
  const label = document.getElementById('label');
  let count = 1;
  
  const states = Object.entries({
    'Inhale': 8, 
    'Pause Inhale': 8, 
    'Exhale': 12, 
    'Pause Exhale': 8
  }).map(([label, duration]) => ({label, duration}));
  let state = 0;
 
  const syncUI = () => {
    label.textContent = states[state].label;
    timer.textContent = count;
  };
  
  syncUI();
  
   // Interval for the timer
  return interval = setInterval(function() {

    // Increment count
    count++;

    // Update the timer display and label
    timer.textContent = count;

    if (count > states[state].duration) {
      state++;
      if(state === states.length){
        state = 0;
      }
      count = 1;
      syncUI();
    }

  }, 1000); // 1000 milliseconds = 1 second
}

// Start the timer initially
startTimer();
body {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
  margin: 0;
  font-family: Arial, sans-serif;
  text-align: center;
}

h1 {
  font-size: 24px;
  font-weight: bold;
}

p {
  font-size: 18px;
}
<h1>Special Timer</h1>
<p id="timer"></p>
<p id="label"></p>
英文:

You could use a finite-state machine (https://en.wikipedia.org/wiki/Finite-state_machine):

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// Function to start the timer
function startTimer() {

  const timer = document.getElementById(&#39;timer&#39;);
  const label = document.getElementById(&#39;label&#39;);
  let count = 1;
  
  const states = Object.entries({
    &#39;Inhale&#39;: 8, 
    &#39;Pause Inhale&#39;: 8, 
    &#39;Exhale&#39;: 12, 
    &#39;Pause Exhale&#39;: 8
  }).map(([label, duration]) =&gt; ({label, duration}));
  let state = 0;
 
  const syncUI = () =&gt; {
    label.textContent = states[state].label;
    timer.textContent = count;
  };
  
  syncUI();
  
   // Interval for the timer
  return interval = setInterval(function() {

    // Increment count
    count++;

    // Update the timer display and label
    timer.textContent = count;

    if (count &gt; states[state].duration) {
      state++;
      if(state === states.length){
        state = 0;
      }
      count = 1;
      syncUI();
    }

  }, 1000); // 1000 milliseconds = 1 second
}

// Start the timer initially
startTimer();

<!-- language: lang-css -->

body {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
  margin: 0;
  font-family: Arial, sans-serif;
  text-align: center;
}

h1 {
  font-size: 24px;
  font-weight: bold;
}

p {
  font-size: 18px;
}

<!-- language: lang-html -->

&lt;h1&gt;Special Timer&lt;/h1&gt;
&lt;p id=&quot;timer&quot;&gt;&lt;/p&gt;
&lt;p id=&quot;label&quot;&gt;&lt;/p&gt;

<!-- end snippet -->

答案4

得分: 0

这段代码正在按照您告诉它的方式执行(当然,这总是如此),因为在您的代码中没有任何内容告诉显示的计数重置。您可以实现这一点的一种方法是拥有一个名为displayedCount的单独计数,当隐藏的计数达到特定数字时重置,如下所示:

// 启动计时器的函数
function startTimer() {
  var timer = document.getElementById('timer');
  var label = document.getElementById('label');
  var count = 1;
  var displayedCount = 1;

  // 计时器的时间间隔
  var interval = setInterval(function() {
    // 更新计时器显示和标签
    timer.textContent = displayedCount;

    if (count <= 8) {
      label.textContent = '吸气';
    } else if (count <= 16) {
      label.textContent = '暂停吸气';
    } else if (count <= 28) {
      label.textContent = '呼气';
    } else if (count <= 36) {
      label.textContent = '暂停呼气';
    }

    // 增加计数
    count++;
    if (count === 9 || count === 17 || count === 29 || count === 37) {
      displayedCount = 1;
    } else {
      displayedCount++;
    }

    // 在达到8、8、12、8模式后停止计时器
    if (count === 45) {
      clearInterval(interval);
      startTimer();
    }
  }, 1000); // 1000 毫秒 = 1 秒
}

// 初始时启动计时器
startTimer();

请注意,这只是代码的翻译部分。

英文:

The code is doing exactly what you are telling it to (which is always the case of course), as there is nothing in your code telling the displayed count to reset. One way you could implement this is to have a separate count called displayedCount that resets when the hidden count reaches specific numbers, like this:

// Function to start the timer
function startTimer() {
  var timer = document.getElementById(&#39;timer&#39;);
  var label = document.getElementById(&#39;label&#39;);
  var count = 1;
  var displayedCount = 1;

  // Interval for the timer
  var interval = setInterval(function() {
    // Update the timer display and label
    timer.textContent = displayedCount;

    if (count &lt;= 8) {
      label.textContent = &#39;Inhale&#39;;
    } else if (count &lt;= 16) {
      label.textContent = &#39;Pause Inhale&#39;;
    } else if (count &lt;= 28) {
      label.textContent = &#39;Exhale&#39;;
    } else if (count &lt;= 36) {
      label.textContent = &#39;Pause Exhale&#39;;
    }

    // Increment count
    count++;
    if (count === 9 || count === 17 || count === 29 || count === 37) {
      displayedCount = 1;
    } else {
      displayedCount++;
    }

    // Stop the timer after reaching 8, 8, 12, 8 pattern
    if (count === 45) {
      clearInterval(interval);
      startTimer();
    }
  }, 1000); // 1000 milliseconds = 1 second
}

// Start the timer initially
startTimer();

huangapple
  • 本文由 发表于 2023年6月1日 02:56:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76376522.html
匿名

发表评论

匿名网友

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

确定