0

i need to restart a setTimeout after stopping it with a clearTimeout the code is like:

function start(){timeout = setTimeout(function(){...}, 1000}
function increment(){interval();}
function stop(){clearTimeout(timeout)}

better explained: function start() is a timeout of 1000ms for another function that recall start(). function increment() just add +1 to a value every 1000s but it doesn't matter with the problem. the last function stop() stops the setTimeout in the function start(). i need to stop the setTimeout in start() for just 1000ms and then let it continue working.

braX
  • 11,506
  • 5
  • 20
  • 33
Maker
  • 13
  • 6
  • What's the question? – Liam Jun 21 '16 at 16:13
  • 1
    i tried with another setTimeout but with this the +1 every second in increment() becomes not regular (it jumps 2-3 points 1 sec, then 4 - 5 points...) – Maker Jun 21 '16 at 16:14
  • `stop(); start();`? – p.s.w.g Jun 21 '16 at 16:15
  • How to stop setTimeout for 1000ms and then let it works normally – Maker Jun 21 '16 at 16:15
  • I dunno how they work... stop() start()...? – Maker Jun 21 '16 at 16:15
  • 1
    It's very hard to understand what you're asking. Can you perhaps put it in a snippet or [jsfiddle](https://jsfiddle.net/)? – p.s.w.g Jun 21 '16 at 16:17
  • Just call `start` after a delay: `function stop() {clearTimeout(timeout); setTimeout(start, 1000);}`. – Teemu Jun 21 '16 at 16:18
  • You can't rely on [setTimeouts to run sequentially](http://stackoverflow.com/a/5998338/542251) or for that matter for them to run exactly within the period defined. The event will be "fuzzy" depending on what else is in the event queue – Liam Jun 21 '16 at 16:19
  • function stop() {clearTimeout(timeout); timer = setTimeout(start, 1000);} kinda working – Maker Jun 21 '16 at 16:21
  • If you want something to run sequentially in JavaScirpt you should use call backs – Liam Jun 21 '16 at 16:22
  • https://jsfiddle.net/th4p68h3/ (id and classes are in italian... sorry for that) here it is the "game" i'm doing.... what i need is add a period of time (1s) in which the footballer stop doing points.... – Maker Jun 21 '16 at 16:27
  • kinda works because the 1s delay stays forever whene i need it for 1 time... – Maker Jun 21 '16 at 16:27
  • @Maker You never said that in your question. Then just create a flag, that the first option has already been used ... – Teemu Jun 21 '16 at 16:28
  • doing so you just add 1000ms to the first setTimeout... so is not what i asked... – Maker Jun 21 '16 at 16:32

1 Answers1

0

ok.... i "avoid" the problem.... i need to stop the setTimeout for 1000ms and then let it works normally... i tried so... instead of pausing the setTimeout I put another time var.

 var time = 1000;
    var timecache = 1000;
    fatica = 3000;
    sudore = 3000;
    function interval() { timeout = setTimeout(increment, time);} //here setTimeout uses var time//
    function increment(){console.log(time);point += 1;document.getElementById("pul").value = point;interval();}
    function fermafatica() {time = timecache;setInterval(ferma, fatica);} //here the function equals time to timecache so vas time is always 1000ms//
    function ferma(){time = 10000; setTimeout(fermafatica, sudore);} // then here I transformed the time in 10000 so the first function will take 10000 instead of 1000 in setTimeout//
//plus i put a setTimeout to recall function fermafatica() that reset the time to 1000//

this i what i want... I avoid the problem and found another way to do that... but it works... thank you anyway

Maker
  • 13
  • 6