Skip to main content

Timing User Behavior

How long a user takes to complete a task is an important metric for researchers. m2c2kit provides a Timer to measure these durations.

How long to click the button?

In the below example, we will measure how long it takes a user to click a button. Click the "Begin" button to advance to the next scene. When the "CLICK ME!" button appears, click it as fast it as you can. A console message will show how long it took from when the button appeared until when you clicked it.

The relevant Timer code appears in two places. First, we start a new timer called rt (for "response time") in the onAppear event for the second scene:

sceneTwo.onAppear(() => {
console.log("sceneTwo onAppear event");
clickMeButton.hidden = false;
clickMeButton.isUserInteractionEnabled = true;
Timer.startNew("rt");
});

Second, we calculate the duration of the timer in the onTapDown event handler for the button:

clickMeButton.onTapDown(() => {
clickMeButton.isUserInteractionEnabled = false;
const clickDurationMs = Timer.elapsed("rt");
Timer.remove("rt");
console.log("You took " + clickDurationMs + " milliseconds");
backButton.hidden = false;
});
tip

To prevent users from pushing the "CLICK ME!" button multiple times within the same trial, we disable user interaction in the onTapDown event handler for the "CLICK ME!" button immediately after it is pushed. We enable the button again in the onAppear event handler for sceneTwo.

Loading...