0

I'm quite new to javascript and I want a the crickit.motor1.run(60) to get set to 0 after 2 seconds but it isn't working and I'm kinda out of options for what to do next. This is my code

forever(function() {
  if (crickit.touch1.touchRead() > 400) {
    light.setPixelColor(0, 0x00ffff)
    crickit.motor1.run(60);
  }

  pause(100)
})

function motorOff() {
  crickit.motor1.run(0);
}

forever(function() {
  if (crickit.motor1.run() = 60)
    setTimeout(motorOff() {

    }, 2000);
})
2
  • 4
    don't call motorOff function, just pass the name of the function to setTimeout --> setTimeout(motorOff, 2000); Commented Sep 5, 2020 at 12:56
  • 2
    crickit.motor1.run() = 60 should be using == or === for comparison. Singular = is for assignment Commented Sep 5, 2020 at 12:59

2 Answers 2

0

These will work:

setTimeout( motorOff, 2000);

setTimeout( 'motorOff()', 2000);

setTimeout( function() { motorOff() }, 2000);

Sign up to request clarification or add additional context in comments.

3 Comments

It still saying that I made a mistake somewhere in this part: forever(function () { if (crickit.motor1.run() == 60) setTimeout(function () { motorOff() }, 2000); }) and I have no idea what that would be
@Snain and there is no error message ?
There is supposed to be if () {line1 line2} if there is no {}, JS adds only the first line to if
0

You have to use it like this: setTimeout(motorOff, 2000);
If you have a function that uses parameters, lets say motorOff(param) would accept 1 paramter, you have to use it like this:
setTimeout(motorOff, 2000, param);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.