{"id":10002,"date":"2022-09-21T19:27:23","date_gmt":"2022-09-21T13:57:23","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-how-to-have-a-setinterval-not-act-like-a-for-loop-closed\/"},"modified":"2022-09-21T19:27:23","modified_gmt":"2022-09-21T13:57:23","slug":"solved-how-to-have-a-setinterval-not-act-like-a-for-loop-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-how-to-have-a-setinterval-not-act-like-a-for-loop-closed\/","title":{"rendered":"[Solved] How to have a setInterval not act like a for loop [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-62851280\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"62851280\" data-parentid=\"62851031\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>The problem is that you&#8217;re not parsing the input to a number. So when you do <code>currTime += 33;<\/code>, it&#8217;s concatenating strings instead of adding numbers. Use <code>parseInt()<\/code> when you read the inputs into the variables.<\/p>\n<\/p>\n<div class=\"snippet\" data-lang=\"js\" data-hide=\"false\" data-console=\"true\" data-babel=\"false\">\n<div class=\"snippet-code\">\n<pre class=\"snippet-code-js lang-js prettyprint-override\"><code>var currTime = 0\nvar origTime = 0\nvar timerFunc\nvar a = document.querySelector('#node1')\nvar i = 0\nvar md\nvar sd\nvar msd\nvar freq = 33;\n\nfunction button1Action() {\n  if (i === 0) {\n    origTime = parseInt(document.getElementById(\"time\").value)\n    currTime = parseInt(document.getElementById(\"time\").value)\n    var b = a.cloneNode(true)\n    b.id = 'node2'\n    b.innerHTML = '&lt;h1 id=\"text\"&gt;&lt;\/h1&gt;&lt;button id=\"button1\" class=\"button1\" onclick=\"button1Action()\"&gt;Start&lt;\/button&gt;&amp;nbsp;&lt;button id=\"button2\" class=\"button2\" onclick=\"button2Action()\"&gt;Clear&lt;\/button&gt;'\n    a.parentNode.replaceChild(b, a)\n    let hd = Math.floor(currTime \/ 3600000)\n    let m = Math.floor((currTime % 3600000) \/ 60000)\n    let s = Math.floor((currTime % 60000) \/ 1000)\n    let ms = Math.floor(currTime % 1000)\n    if (m &lt; 10) {\n      md = `0${m}`\n    } else {\n      md = `${m}`\n    }\n    if (s &lt; 10) {\n      sd = `0${s}`\n    } else {\n      sd = `${s}`\n    }\n    if (ms &lt; 10) {\n      msd = `00${ms}`\n    } else if (ms &lt; 100) {\n      msd = `0${ms}`\n    } else {\n      msd = `${ms}`\n    }\n    document.getElementById(\"text\").innerHTML = `${hd}:${md}:${sd}.${msd}`\n  } else if (i &gt;= 1 &amp;&amp; (i % 2) == 1) {\n    timerFunc = setInterval(function() {\n      currTime += freq\n      let hd = Math.floor(currTime \/ 3600000)\n      let m = Math.floor((currTime % 3600000) \/ 60000)\n      let s = Math.floor((currTime % 60000) \/ 1000)\n      let ms = Math.floor(currTime % 1000)\n      if (m &lt; 10) {\n        md = `0${m}`\n      } else {\n        md = `${m}`\n      }\n      if (s &lt; 10) {\n        sd = `0${s}`\n      } else {\n        sd = `${s}`\n      }\n      if (ms &lt; 10) {\n        msd = `00${ms}`\n      } else if (ms &lt; 100) {\n        msd = `0${ms}`\n      } else {\n        msd = `${ms}`\n      }\n      document.getElementById(\"text\").innerHTML = `${hd}:${md}:${sd}.${msd}`\n    }, freq)\n    document.getElementById(\"button1\").innerHTML = `Pause`\n    document.getElementById(\"button1\").style.backgroundColor = \"#0000ff\"\n  } else if (i &gt;= 1 &amp;&amp; (i % 2) == 0) {\n    clearInterval(timerFunc)\n    document.getElementById(\"button1\").innerHTML = `Cont..`\n    document.getElementById(\"button1\").style.backgroundColor = \"#00ff00\"\n  } else {\n    throw \"Error: i must be a positive integer or 0\"\n  }\n  i++\n}\n\nfunction button2Action() {\n  clearInterval(timerFunc)\n  currTime = origTime\n  let hd = Math.floor(currTime \/ 3600000)\n  let m = Math.floor((currTime % 3600000) \/ 60000)\n  let s = Math.floor((currTime % 60000) \/ 1000)\n  let ms = Math.floor(currTime % 1000)\n  if (m &lt; 10) {\n    md = `0${m}`\n  } else {\n    md = `${m}`\n  }\n  if (s &lt; 10) {\n    sd = `0${s}`\n  } else {\n    sd = `${s}`\n  }\n  if (ms &lt; 10) {\n    msd = `00${ms}`\n  } else if (ms &lt; 100) {\n    msd = `0${ms}`\n  } else {\n    msd = `${ms}`\n  }\n  document.getElementById(\"text\").innerHTML = `${hd}:${md}:${sd}.${msd}`\n  document.getElementById(\"button1\").innerHTML = `Start`\n  document.getElementById(\"button1\").style.backgroundColor = \"#00ff00\"\n  i = 1\n}<\/code><\/pre>\n<pre class=\"snippet-code-css lang-css prettyprint-override\"><code>.button1 {\n  background-color: #00ff00;\n  color: #999999;\n}\n\n.button2 {\n  background-color: #ff0000;\n  color: #eeeeee\n}\n\n.in1::placeholder {\n  color: #000e;\n}<\/code><\/pre>\n<pre class=\"snippet-code-html lang-html prettyprint-override\"><code>&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n\n&lt;head&gt;\n  &lt;title&gt;Stopwatch&lt;\/title&gt;\n&lt;\/head&gt;\n\n&lt;body id=\"node1\"&gt;\n  &lt;input type=\"number\" class=\"in1\" id=\"time\" placeholder=\"0\" value=\"0\"&gt;\n  &lt;br\/&gt;&lt;br\/&gt;\n  &lt;button class=\"button1\" onclick=\"button1Action()\"&gt;Set&lt;\/button&gt;\n&lt;\/body&gt;\n\n&lt;\/html&gt;<\/code><\/pre>\n<\/div>\n<\/div><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">0<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved How to have a setInterval not act like a for loop [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] The problem is that you&#8217;re not parsing the input to a number. So when you do currTime += 33;, it&#8217;s concatenating strings instead of adding numbers. Use parseInt() when you read the inputs into the variables. var currTime = 0 var origTime = 0 var timerFunc var a = document.querySelector(&#8216;#node1&#8217;) var i = 0 &#8230; <a title=\"[Solved] How to have a setInterval not act like a for loop [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-how-to-have-a-setinterval-not-act-like-a-for-loop-closed\/\" aria-label=\"More on [Solved] How to have a setInterval not act like a for loop [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[346,333],"class_list":["post-10002","post","type-post","status-publish","format-standard","hentry","category-solved","tag-html","tag-javascript"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] How to have a setInterval not act like a for loop [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-how-to-have-a-setinterval-not-act-like-a-for-loop-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] How to have a setInterval not act like a for loop [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] The problem is that you&#8217;re not parsing the input to a number. So when you do currTime += 33;, it&#8217;s concatenating strings instead of adding numbers. Use parseInt() when you read the inputs into the variables. var currTime = 0 var origTime = 0 var timerFunc var a = document.querySelector(&#039;#node1&#039;) var i = 0 ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-how-to-have-a-setinterval-not-act-like-a-for-loop-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-21T13:57:23+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-have-a-setinterval-not-act-like-a-for-loop-closed\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-have-a-setinterval-not-act-like-a-for-loop-closed\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] How to have a setInterval not act like a for loop [closed]\",\"datePublished\":\"2022-09-21T13:57:23+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-have-a-setinterval-not-act-like-a-for-loop-closed\/\"},\"wordCount\":62,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"html\",\"javascript\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-have-a-setinterval-not-act-like-a-for-loop-closed\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-how-to-have-a-setinterval-not-act-like-a-for-loop-closed\/\",\"name\":\"[Solved] How to have a setInterval not act like a for loop [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-09-21T13:57:23+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-have-a-setinterval-not-act-like-a-for-loop-closed\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-how-to-have-a-setinterval-not-act-like-a-for-loop-closed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-have-a-setinterval-not-act-like-a-for-loop-closed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] How to have a setInterval not act like a for loop [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] How to have a setInterval not act like a for loop [closed] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-how-to-have-a-setinterval-not-act-like-a-for-loop-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] How to have a setInterval not act like a for loop [closed] - JassWeb","og_description":"[ad_1] The problem is that you&#8217;re not parsing the input to a number. So when you do currTime += 33;, it&#8217;s concatenating strings instead of adding numbers. Use parseInt() when you read the inputs into the variables. var currTime = 0 var origTime = 0 var timerFunc var a = document.querySelector('#node1') var i = 0 ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-how-to-have-a-setinterval-not-act-like-a-for-loop-closed\/","og_site_name":"JassWeb","article_published_time":"2022-09-21T13:57:23+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-have-a-setinterval-not-act-like-a-for-loop-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-have-a-setinterval-not-act-like-a-for-loop-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] How to have a setInterval not act like a for loop [closed]","datePublished":"2022-09-21T13:57:23+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-have-a-setinterval-not-act-like-a-for-loop-closed\/"},"wordCount":62,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["html","javascript"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-have-a-setinterval-not-act-like-a-for-loop-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-how-to-have-a-setinterval-not-act-like-a-for-loop-closed\/","name":"[Solved] How to have a setInterval not act like a for loop [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-09-21T13:57:23+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-have-a-setinterval-not-act-like-a-for-loop-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-how-to-have-a-setinterval-not-act-like-a-for-loop-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-have-a-setinterval-not-act-like-a-for-loop-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] How to have a setInterval not act like a for loop [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/10002","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=10002"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/10002\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=10002"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=10002"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=10002"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}