Timer functions


#1

Hello guys,

I didn’t reach to implement my Stopwatch in ZapWorks Studio.

In fact, the js functions: setInterval() and clearInterval() can’t be find.

Please, help :slight_smile:


#2

Hi Loick,

We don’t use those functions as they are part of the window object which we don’t expose in ZapWorks Studio.

We have however implemented our own versions, namely z.every( ) and z.everyOff( ).

Check out our other utility functions here - https://docs.zap.works/studio/scripting/utility-functions/

Thanks,
Mark


#3

Dear Mark
Is there any examples for how to do this? I would like to do a timer countdown.
Thank you.
Milenne


#4

I have a simple 10 sec timer I made for my Jack’s Revenge game.
Is that what your looking for?
Steve


#5

For my game, I ended up creating an over-arching script in the root node that had a simple function which incremented a variable I called timer. I then used Z.every to call that function to create a system clock.

The trick is feeding it out to other scripts and for that you need to export that variable using

export let

when you declare. I’ve had mixed success. It’s a work in progress.


#6

Yes, I have managed to do it. I have got an example script from Jvuiloon.


THank you. I have another question which I will put in a new thread.:wink:


#7

Hey everyone
I have one more question if anyone could please help if you can of course.

I have the timer code below.

What I want to do it to add an if timer is equal 0, then switch certain things off and stop the game. I have tried but it goes off right at the beginning of the game…?

That is the code as the original

//Set Timer
var timer = 90;

parent.on(“pointerdown”, (e) => {
Z.every(1000,myTimer);
});

function myTimer(){
symbol.nodes.debug.text("timer: "+timer);

if(timer--<=0)Z.everyOff(myTimer); 

}

Thank you
Milenne


#8

Didi you try adding a call to a function:

function myTimer(){
    if(timer--<=0){
    Z.everyOff(myTimer); 
    stopGame();
    }
}

function stopGame(){
// do what you have to do...
}

#9

Thanks jvouillon
Yes, that is what I have there. When it gets to zero the timer stops. I have then added lines to stop the game.

Something like:

if(timer–<=0){
Z.everyOff(myTimer);
symbol.controllers.Question1CNT.elements.n1_QuestionOff.activate();
}

But then for some reason it stops the game at the beginning rather than the end. It stops when the timer is 90 and not zero…


#10

Just to be sure it is not a syntax error, could you check your condition:
what I read in yours, is:

if(timer–<=0)

instead of:

if(timer--<=0)


#11

It is if(timer–<=0)

//Set Timer
var timer = 90;

parent.on(“pointerdown”, (e) => {
Z.every(1000,myTimer);
});

function myTimer(){
symbol.nodes.debug.text("timer: "+timer);

if(timer--<=0)Z.everyOff(myTimer); 

// stopGame();

}
When I put the stopGame, it stops at the beginning…

Then I added an extra one:
if(timer==0)
stopGame();
it works but then when press the play again the timer doesn’t go back to 90…


#12

You need the curly brackets to have more than one instruction with the if statement:

if(timer--<=0)Z.everyOff(myTimer);  
stopGame(); // out of the if statement


if(timer--<=0){
Z.everyOff(myTimer); 
stopGame(); // in the if statement
}

#13

I see. Yes, the game ends :wink:
Now when I press the button play again the timer doesn’t go back to 90…;-(

I have a button play again which it goes back to instruction screen so you enter the same PLay button as before but the times goes to -1 or -2. oh dear!!!


#14

Just reset the timer value to 90, in your btn playAgain…


#15

ok. I will try that. THank you. Basic coding knowledge showing :smiley:


#16

The play again button just links to the starter screen which then when I press play there is the var=90… so why doesn’t that reset when I press play ?


#17

It seems that your instruction:

var timer = 90;

is outside the parent.on(“pointerdown”… ) function

To make things simple, the instructions in a script are executed just one time, basically when the app is loaded.
If you want instructions to be executed several times, you have to put them in a function and call the function…

So, for example, one solution could be:

var timer; // the timer variable is created one time,,,

parent.on(“pointerdown”, (e) => {
timer = 90; // the timer variable is set to 90 each time the button is pressed...
Z.every(1000,myTimer);
});

#18

Ha! Thank you jvouillon.
It worked :wink:

Have a great day.
Milenne