2

I'm trying to create a metronome with Typescript.

I have this javascript code:

(function theLoop (i) {
        setTimeout(function () {
            metronome.play();
            if (--i) {
                theLoop(i);
            }
        }, 3000);          // interval set to 3000
    })(10);                // play it 10 times

And I wanted to convert it into Typescript code. Unfortunately I don't know how to do this (espacially regarding the last line => })(10);

Can someone help me with this?

Taremeh
  • 302
  • 2
  • 4
  • 13
  • TypeScript is just a superset of JavaScript. On the other hand, the metronome should probably have a way of changing it's tempo and a way of starting it and stopping it as well as making it tick on demand. Basically I'd expect `metronome.changeTempo(bpm); metronome.onTick(function () { if (...) metronome.stop(); }); metronome.play();`. You could also have a built-in feature specific to ticking a number of times. `metronome.tick(10)`. – plalx Dec 01 '16 at 19:56
  • @plalx I like your idea! What would the code look like? Or: How can I approach it? – Taremeh Dec 01 '16 at 20:18
  • Do you know a bit of OO? Look at the `class` keyword and examples on how to use it. Metronome would be a class here, not an Audio file. It could take an audio as a constructor parameter though.. `let metronome = new Metronome(new Audio('tick.mp3'))`. – plalx Dec 01 '16 at 20:24
  • @plalx Do you know a tutorial regarding this topic? Because I'm unfortunately not this far in OO... – Taremeh Dec 01 '16 at 20:57

2 Answers2

3

As everyone said, typescipt is a superset of javascript so your code is valid typescript, but here's how to do it with an arrow function (which is also es6 javascript) and types:

(function theLoop (i: number) {
        setTimeout(() => {
            metronome.play();
            if (--i) {
                theLoop(i);
            }
        }, 3000);
    })(10);

(code in playground)

And here's another variation:

let theLoop: (i: number) => void = (i: number) => {
    setTimeout(() => {
        metronome.play();
        if (--i) {
            theLoop(i);
        }
    }, 3000);
};

theLoop(10);

(code in playground)


Edit

Using the 2nd option I gave you, changing the delay is easy:

let theLoop: (i: number, delay?) => void = (i: number, delay = 3000) => {
    if (i % 2 === 0) {
        delay = 1500;
    }

    setTimeout(() => {
        metronome.play();
        if (--i) {
            theLoop(i);
        }
    }, delay);
};

theLoop(10);

(code in playground)

Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299
  • Thanks! But is there any way to change the interval (3000) after I started theLoop? Because now, when I change the number, theLoop still continues until it played the sound 10 times... – Taremeh Dec 01 '16 at 20:51
  • Wow, this looks amazing. Unfortunately I still get a second overlapping metronome, if I change the delay/interval. Also, the performance is now really bad (the delays aren't strictly in time)... – Taremeh Dec 01 '16 at 21:10
  • I don't know what you're trying to do so I can't help you with that. You wanted to know how to convert your js code to ts, and that's what I answered. If you have some specific in mind then better ask it, probably as a new question if it's not related to your original one. – Nitzan Tomer Dec 01 '16 at 21:26
  • Yeah, thanks. I opened a new question and gave more information: http://stackoverflow.com/questions/40920820/typescript-metronome Thanks for your help! – Taremeh Dec 01 '16 at 21:36
1

Typescript is a upperset of Javascript. So you can copy your code into Typescript and it will work

Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112