How does time code work?

Fill this out this info to make it easier for the community to help you! If you don’t fill out this information, your post may be deleted or removed.

What Wick Editor Version are you using?
The one that opens when you click “Browser”

Describe the Problem
How does time code work? I want to make it so that every 4 seconds, some code is activated. This has to happen inside an object (Ex. a drawing made into a clip) and not in the Update coding menu. I want to know if there is a code block that does time related functions.

What have you tried so far?
I honestly don’t know where to start. I have checked many posts about timers and counters and I can’t find any specifically relating to my issue.

To make a timer you just need to make a variable and, keep adding to that variable resetting it to 0 when it reaches a number, and just check when it gets to a number.
Timer1-12-2025_17-03-01.wick (2.0 KB)

1 Like

This is a great question!
I know a few methods to work with time. I’ll start by sharing the more precise approach using date objects, then I’ll share a second, simpler method using the project’s FPS (same as @Boxy’s approach).


1. Date Objects

The Date(...) function takes a date inside of the parenthesis, and uses that to create a date object. For example:

var newYears = new Date("2025-01-01"); // year - month - day

There are also many ways to input a date, and you can get precise down to the millisecond!
If you’re interested, then look at this website to learn more:


If you don’t plug anything in for the date, then it’ll instead set the variable to the current date and time, including the seconds. To give it a test, run this code in a default script:
var currentTime = new Date(); 
alert(currentTime); // WEEKDAY MONTH DAY YEAR HR:MIN:SEC TIMEZONE

Now with date objects, you can specifically get the minutes, hours, seconds, etc., individually, like this.

For your game, you’d probably just need to know the seconds. If you’re interested in learning about the other time components, you can learn about em’ here

To create a timer, start off by creating a window variable in the default script for the starting seconds, and another variable for counting the time that starts at zero.

window.time = new Date().getSeconds();
window.timeCounter = 0;

Note: window variables are useful because you can reference them from anywhere in your code!
And then in the update script, check whenever your time variable is not equal to the current time. If that’s true then update the time.

// check if time changed
if(time !== new Date().getSeconds()){
    // update time
    time = new Date().getSeconds();
    // increase timer
    timeCounter++;
}

And now, you have a working timer! If you want it so that something happens every 4 seconds, you can either do an if statement to see if timeCounter equals to 4, then reset it to zero

// check if time changed
if(time !== new Date().getSeconds()){
    // update time
    time = new Date().getSeconds();
    // increase timer
    timeCounter++;
    if(timeCounter == 4){
        // DO ANYTHING
        // OVER HERE!!

        timeCounter = 0; // reset timer
    }
}

Or you can just check modulo by 4 using % operator, which you can learn more about here.
With this method you won’t need to reset the timer.

// check if time changed
if(time !== new Date().getSeconds()){
    // update time
    time = new Date().getSeconds();
    // increase timer
    timeCounter++;
    if(timeCounter%4 == 0){
        // DO ANYTHING
        // OVER HERE!!
    }
}

And there you have it :)
Working example: timeExample.wick (2.2 KB)


2. Project Frame rate

Now, you’re probably familiar with the term FPS, Frames per Second. In other words, FPS is how many times your code will run per second.

This second method (no pun intended) will rely on the project’s frame rate, which might make it less accurate than the first though it still works pretty well.

To start off, create a timer variable in a default script:

window.timer = 0;

Then, inside the update script, increase the timer by the reciprocal of the frame rate

timer+ = 1/project.framerate;

Note: You can find the project.framerate in the reference code. Default frame rate is 12, you can also update it inside the project settings to make your game run faster!
And… with just these two lines of code, you have a timer :0

Now though it’s a bit of an ugly timer since you’re increasing it by a long decimal :sweat_smile:
You can round the timer using Math.round(timer) or timer.toFixed(…) methods (the latter for more precise decimal place).

Another way you can do it is instead increase the timer by 1, and check whenever 4 seconds passed (update script)

timer ++;
var seconds = timer/project.framerate;
if(seconds%4 == 0){
// CODE HERE WILL RUN EVERY 4 SECONDS
}

A quick explanation on how this works— say you have an fps of 12. This means that your code will run 12 times per second. After 1 second, your timer will be 12, and after 2 seconds it’ll become 24. This means that your timer will equal to 48 after 4 seconds.
So you can divide the timer by the project’s framrate to get the seconds. And similar to the last method, I used the modulo operator to check every 4 seconds.

This is the same way @Boxy’s code they shared here works.
I’ll still share a second working example: timeExample2.wick (2.2 KB)