Question about Javascript?

Hello,
in the game catchingBirds there is the javascript “function timelineEventHandler(doc, e)”. For a project I’m looking for a way to stop this timer with a mouse click and then end it. I would be very grateful for useful tips, since I’ve been spending some time with it, unfortunately without any results.
greeting
Oscar

Hi Oskar,

You can store the timer id returned by setInterval in the doc global object, and pass it to clearInterval function in your mouse click event.

// start the timer in a function / event handler
doc.myTimerId = setInterval(yourFunction, yourInterval);

// stop the timer in another function / event handler
if (doc.myTimerId) {
  clearInterval(doc.myTimerId);
  delete doc.myTimerId;
}

Regards

Hi,
Great, that was the impetus I was looking for. Sometimes you can no longer see the forest in front of many trees. Thanks.
greeting
Oscar

1 Like

Hello Toan Le,

I have another question about my problem, with the command “clearInterval(timeinterval);”
I can “pause” the timer, so the timer display stops( as desired) and
can call the next scene. But if I stay in the scene, I noticed,
that the timer display is stopped, but the timer itself continues to run in the background?
I tried it with the command “delete Interval(timeinterval);”, but this shows no effect?
What am I doing wrong?

thanks
Oskar

Translated with DeepL Translate: The world's most accurate translator (free version)

Hi Oskar,

clearInterval will cancel the timer completely.
I’m not sure why you think that the timer still runs in the background.
Please describe your problem in more detail and share your project package (File > Save As > Package) so that I can help.

Regards

Hello Toan Le,
thank you for your time. In the example you will see what I mean. 1. the timer is started(blue Button), runs down and triggers an action (the lower green one).
2. the timer is stopped(red Button), triggers an action (the upper yellow one).
But now you can see, after the remaining seconds, that the lower green action is executed anyway. So I assume that the timer is still running?
thank you
Oskar
mytimer.saolapack (2.7 KB)

Hi,

You didn’t use clearTimeout(timeout_timer_id) to clear the timeout timer that starts the timeline after 10s so it still runs.

However, you don’t need to use JavaScript in your project, using predefined actions are enough.
Please see your project fixed: mytimer_fixed.saolapack (3.0 KB)

Regards

Hello Toan Le,

yes, that is also a possibility, simple and easy, I don’t know why I haven’t
I have not yet thought of it myself. Maybe you could answer me one more question about Java, what exactly is the “timer_id”? Could you give me an example?

Thanks for your time
Oskar

Hi Oskar,

Timer id is the return value when calling a timer function (setTimeout or setInterval).
You’ll use it to stop the timer (with clearTimeout or clearInterval, respectively).
You can see a tutorial for beginners about these functions here: JavaScript Timing Events

Regards

Hi Toan Le,

thanks for the link…

Oskar