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);
})
adiga
  • 34,372
  • 9
  • 61
  • 83
Snain
  • 1
  • 4
    don't call `motorOff` function, just pass the name of the function to `setTimeout` --> `setTimeout(motorOff, 2000);` – Yousaf Sep 05 '20 at 12:56
  • 2
    `crickit.motor1.run() = 60` should be using `==` or `===` for comparison. Singular `=` is for assignment – Nick Parsons Sep 05 '20 at 12:59

2 Answers2

0

These will work:

setTimeout( motorOff, 2000);

setTimeout( 'motorOff()', 2000);

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

V.Volkov
  • 731
  • 3
  • 12
  • 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 Sep 05 '20 at 13:04
  • @Snain and there is no error message ? – Mister Jojo Sep 05 '20 at 13:07
  • There is supposed to be `if () {line1 line2}` if there is no `{}`, JS adds only the first line to `if` – V.Volkov Sep 05 '20 at 14:04
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);

Alexandru DuDu
  • 998
  • 1
  • 7
  • 19