{"id":1409,"date":"2015-04-21T05:43:33","date_gmt":"2015-04-21T05:43:33","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=1409"},"modified":"2015-08-06T12:19:49","modified_gmt":"2015-08-06T12:19:49","slug":"how-to-reconfigure-settimeout-on-the-fly","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/stack-overflow\/how-to-reconfigure-settimeout-on-the-fly\/","title":{"rendered":"How to reconfigure setTimeout on the fly"},"content":{"rendered":"<p><a href=\"http:\/\/stackoverflow.com\/users\/534755\/nikola\"><img loading=\"lazy\" decoding=\"async\" title=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" src=\"http:\/\/stackoverflow.com\/users\/flair\/534755.png\" rel=\"lightbox[1409]\" alt=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" width=\"208\" height=\"58\" \/><\/a><br \/>\nI&#8217;m a big fan of <a href=\"http:\/\/stackoverflow.com\/\">Stack Overflow<\/a> and I tend to contribute regularly (am currently in the <a href=\"http:\/\/stackexchange.com\/leagues\/1\/alltime\/stackoverflow\/2008-07-31\/534755?sort=reputationchange#534755\">top 0.X%<\/a>).\u00a0In this category (<a href=\"http:\/\/www.nikola-breznjak.com\/blog\/category\/stack-overflow\/\">stackoverflow<\/a>)\u00a0of posts I will will be posting my top rated questions and answers. This, btw, is allowed as explained in the meta thread <a href=\"http:\/\/meta.stackoverflow.com\/questions\/266971\/can-i-post-so-questions-and-answers-in-a-personal-blog\/266973\">here<\/a>.<\/p>\n<p>My <a href=\"http:\/\/stackoverflow.com\/questions\/6729889\/how-to-reconfigure-settimeout-on-the-fly\">question<\/a> was:<\/p>\n<p>I was wondering if there is a way to set the setTimeout function to different timeout on the fly.<\/p>\n<p>So, for example I check something every 1 second, but if I don&#8217;t get an expected result in like 10 seconds I want to &#8220;reconfigure&#8221; this setTimeout to wait 3 seconds instead of 1.<\/p>\n<p>Here is my code:<\/p>\n<pre class=\"lang:default decode:true\">var br = 0;\r\nvar waitInterval = 1000;\r\nvar sleepInterval = 2000;\r\n\r\nvar waitForNewRace = setInterval(\r\nfunction checkForNewRace(){\r\n    if ( $(\"#data\").html() == \"1\"){        \r\n        $(\"#res\").html(\"got it!\");\r\n    }\r\n    else{\r\n        $(\"#counter\").html(br);\r\n        if (br++ &gt; 9)\r\n            waitInterval = 3000;            \r\n    }                   \r\n\r\n    $(\"#tst\").html(waitInterval);\r\n},\r\nwaitInterval\r\n);<\/pre>\n<p>If you want to check it out here is the mentioned code on jsfiddle: <a href=\"http:\/\/jsfiddle.net\/Hitman666\/Vyczj\/2\/\" rel=\"nofollow\">http:\/\/jsfiddle.net\/Hitman666\/Vyczj\/2\/<\/a><\/p>\n<p style=\"color: #222222;\">\u00a0The answer, by <a href=\"http:\/\/stackoverflow.com\/users\/58186\/kooiinc\">KooiInc<\/a>,\u00a0was:<\/p>\n<blockquote><p>You have to stop the interval and restart it. See <a href=\"http:\/\/jsfiddle.net\/KooiInc\/XFgfR\/\" rel=\"nofollow\">this fork<\/a> of your jsfiddle.<\/p>\n<p>EDIT: I&#8217;ve copied your code here in case something happens with your jsfiddle code:<\/p>\n<pre class=\"lang:default decode:true  \">var br = 0;\r\nvar waitInterval = 1000;\r\nvar sleepInterval = 2000;\r\n\r\n\r\nfunction checkForNewRace(){\r\n    if ( $(\"#data\").html() == \"1\"){        \r\n        $(\"#res\").html(\"got it!\");\r\n    }\r\n    else{\r\n        $(\"#counter\").html(br);\r\n        if (br++ &gt; 5){\r\n            clearInterval(waitForNewRace);\r\n            waitInterval += 1000;\r\n            if (waitInterval &gt; 10000)\r\n                waitInterval = 10000;\r\n\r\n            waitForNewRace = setInterval(\r\n                 checkForNewRace,\r\n                 waitInterval\r\n            );  \r\n        }\r\n    }                   \r\n    $(\"#tst\").html(waitInterval);\r\n}\r\n\r\nvar waitForNewRace = setInterval(\r\n    checkForNewRace,\r\n    waitInterval\r\n);<\/pre>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m a big fan of Stack Overflow and I tend to contribute regularly (am currently in the top 0.X%).\u00a0In this category (stackoverflow)\u00a0of posts I will will be posting&hellip;<\/p>\n","protected":false},"author":1,"featured_media":609,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-1409","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-stack-overflow"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1409","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=1409"}],"version-history":[{"count":2,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1409\/revisions"}],"predecessor-version":[{"id":1683,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1409\/revisions\/1683"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/609"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=1409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=1409"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=1409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}