How to make delays ?

Posted on
  • I'm trying to make a compass that tells the user it's heading through vibration patterns ( one for each cardinal direction ). Thing is, I need to make the reading THEN send the vibration pattern some time after, because the magnetometer goes crazy when trying to read while the vibrator is on.
    So, how can I make it so that it does something like "read heading, wait 0.3s then send vibration" ?

  • You can use setTimeout:

    read_heading();
    setTimeout(send_vibration, 300); // No brackets() after send_vibration!
    
  • Best is maybe to use a promise. You could probably do with setTimeout as well.

    rigrig beat me to it!

  • Okay, yeah, setTimeout sounds good ! How can I make it so that the reading only happen once every X seconds, too ? Wouldn't setTimeout make it run in loop and do the reading of the loop X seconds back ?

  • setInterval

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

How to make delays ?

Posted by Avatar for Jaeth @Jaeth

Actions