Adding running time to many scenes

Hi there,

I am quite new to Hype and my last experience with Javascript is a while ago as well.
I am trying to add the current running time to many of my scenes. I’ve already tried many things including setInterval or setTimeout. But nothing seems to work. I also tried to add the code in an innerHTML element. But this didn’t work either.

Further I’d like to know how it is possible to use one function in many scenes when you’re not allowed to use the same id in different scenes.

Thanks in advance. I might have a silly error in reasoning but I just can’t figure it out.

The following code is from A Book About Hype… …in the “Timelines” section of the “JavaScript” chapter.

var seconds = hypeDocument.currentTimeInTimelineNamed("Main Timeline");​ 
hypeDocument.getElementById("text").innerHTML = seconds.toFixed(2);

Basically, that uses the Hype JavaScript API to get the current time in a timeline. In this example, it’s the “Main Timeline”. That value is then displayed to a text element, with the unique ID of “Text”. Lots of decimal places are used, so the variable “seconds” is shortened to two decimal places.

1 Like

The phrase “running time” might have been confusing.
I mean the actual time (e.g. now 11:41)

It works that it shows the current time when loading the scene but it doesn’t change. So the time isn’t “running”.

OK. now that that’s clear :slight_smile:

One of the ways to have the same thing over multiple scenes is to use a persistent symbol

Within this symbol you can create a javascript function to run on symbol load that updates the time

Yes, I tried it with using a symbol but it doesn’t work either.

And still the time isn’t updating. It loads the current time once when the scene is loaded and that’s it.

You have to set an interval to fire every second (if you want it live) make sure to clear the interval on scene unload so it doesn’t effect performance (maybe)

Would you like me to show you?

Yes please!

• There is a persistent symbol that is viewable on all scenes and has a function “running” on symbol load that creates a new Date() and returns the hours, minutes and seconds.

• Below is a copy of the document as a template and below that there is the symbol itself which you can import into your documents (using the Symbol->Import Symbol… option in the menu) (38.3 KB) (38.0 KB)

Thank You!
I’m gonna test it right away.

But does it have to be a persistent symbol? And if, why?
I already got a usual symbol on each scene. But I can’t use those, can I?

I changed every symbol to a persistent symbol. Now it’s working!

For it to function over every scene, using a persistent symbol is the easiest.
If you use a standard symbol then you would have to add a different symbol with different ID’s for the text element each time. (all elements must have a unique ID)

Because persistent symbols live outside the scene environment you can add them to many scenes (but only 1 per scene you cannot have the same one duplicated in a scene)

So, to summerize, use

• persistent symbols when you want the same thing to appear across many scenes (like a menu)
• standard symbols when you want the same thing to appear throughout a scene

1 Like