-1
myFunction1(state, message){
    if(state) action1;
    if(message) action2;
    work every 0.5 sec
    if(err) throw e;
}

myFunction2(state, message){
    blah blah
    works every 0.3 sec
    if(err) throw e;
}

How to make function1 work every 0.5 sec and function2 work every 0.3 sec?

Is there just one way that use for loop?

Andrew Li
  • 55,805
  • 14
  • 125
  • 143
Alta istar
  • 411
  • 1
  • 6
  • 11
  • 1
    https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html or are you asking about Javascript? – Scary Wombat Aug 10 '16 at 04:43
  • 1
    java or javascript? the two are **NOT** interchangeable - looks like javascript - if so,` setInterval` and/or `setTimeout` are your friends – Jaromanda X Aug 10 '16 at 04:44
  • 1
    [JS timers](https://developer.mozilla.org/en-US/Add-ons/Code_snippets/Timers), in particular [`setInterval`](https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setInterval). – Amadan Aug 10 '16 at 04:46
  • 2
    Possible duplicate of [javascript setInterval](http://stackoverflow.com/questions/5801543/javascript-setinterval) – Ran Hassid Aug 10 '16 at 04:47
  • @AndrewL. that's sort of a bad analogy. "Cop" is short for "copper" and they both mean the same thing. – Mulan Aug 10 '16 at 04:52
  • @naomik First time I've heard that before, always thought of cop as in the police, will change – Andrew Li Aug 10 '16 at 04:52
  • @AndrewL. I think the saying originated as "Java is to JavaScript as car is to carpet" – Mulan Aug 10 '16 at 04:53
  • @naomik Believe so, though there's a treasure trove [here](http://javascriptisnotjava.io) – Andrew Li Aug 10 '16 at 04:54
  • I think I like this one better tho: Java is to JavaScript as cancer is to Coca-Cola – Mulan Aug 10 '16 at 04:54
  • @AndrewL. less of a treasure trove and more of a pile of internet garbage. – Mulan Aug 10 '16 at 04:57

1 Answers1

0

You can use setInterval(function, timeInterval) if you want it to strictly follow the time. I mean if you want the function to be executed even if the function is still running.

Otherwise you can use setTimeout(function, timeInterval) and the call the same function at the end. But this does not strictly ensure the timeInterval since the execution time of the code before the setTimeout is not counted. E.g.

test = function()
{
    //Your code
    setTimeout(test, 1000);
}
Chaitanya Sama
  • 330
  • 3
  • 13