1

Currently, I am using an interval to find out whether the introductary music of my game has finished.

 Musictimeout = setInterval(function () {
                if (Initialmusic.currentTime > 313) {
                    Loopingmusic.play();
                    Loopingmusic = true;
                    clearInterval(Musictimeout);
                }
 }, 10);

However, this slows down the gameplay quite consideably until the interval is cleared. Can I use eventlisteners instead of an interval in this situation?

  • Yes. But what kind of event are you looking for? Lke click, scroll, etc. – Craftein Feb 15 '14 at 15:21
  • possible duplicate of [Javascript Object.Watch for all browsers?](http://stackoverflow.com/questions/1029241/javascript-object-watch-for-all-browsers) – marionebl Feb 15 '14 at 15:21
  • That code really shouldn't cause much of a slowdown, though you're running the timer pretty fast. How laggy does it feel when you use a 100 or even 150 millisecond interval? (Actually I guess that access of `currentTime` *might* be expensive, depending on what "Initialmusic" is.) – Pointy Feb 15 '14 at 15:24
  • @Pointy Initialmusic is a audio object – Ben Johnson mk2 Feb 15 '14 at 15:26
  • 3
    Assuming you use html5 `audio` element, just subscribe to its `onended` event: https://developer.mozilla.org/en-US/docs/Web/Reference/Events/ended_%28Web_Audio%29 – Tommi Feb 15 '14 at 15:32
  • @BenJohnsonmk2 OK well then I guess it might be the case that accessing that property would require some work. I don't know of a way to request events at particular points in playback; there are events generated (according to spec) but mostly at end-of-track and things like that. – Pointy Feb 15 '14 at 15:35
  • As Tommi said can't we use `onended` event for that purpose? – Suman Bogati Feb 15 '14 at 15:37

1 Answers1

2

In answer to your main question, there are no generic, built-in event listener that will fire when the value of a variable in javascript changes.

But, if your variable is time related (which a property named currentTime sounds like it might be, then you could probably use a cleverly set setTimeout() to know exactly when to check it's value rather than trying to check every 10ms like you're doing now. We'd have to know more about what exactly you're trying to do and what exactly Initialmusic.currentTime is to know what better to suggest. For example, if you want to know when a particular point in playback is hit, you should be able to register for the start/stop events and just keep your own timer going that should tell you approximately when you get to the desired playback time.

If you are just waiting until the audio completes, then you can just register for the audio event for when it has ended which is the "ended" event. See here for media events.

In addition, for modern browsers only, if you control the creation of Initialmusic.currentTime, then you could define a setter function that would trigger a notification to you any time its value is changed. See this code example: https://gist.github.com/eligrey/384583. I don't know if this type of solution would work for an audio host object (I'm guessing it probably would not). You would have to try it in multiple modern browsers to see.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • 1
    It looks like it's an HTML audio element, and since he just needs end-of-track notification it's probably even simpler (well, in browsers that actually do it right anyway :) – Pointy Feb 15 '14 at 15:36
  • @Pointy - yep, I've added that to my answer (3rd paragraph) as I noticed a little more about what they were potentially doing. It's always amazing to me that people ask questions about the solution direction they are currently investigating rather than actually describing the real problem. They miss out on so many more ideas when the actual problem is kept hidden. – jfriend00 Feb 15 '14 at 15:37