Help

Re: Record timer script

2910 3
cancel
Showing results for 
Search instead for 
Did you mean: 

Inspired by this discussion, I wrote a script for the Scripting block that lets you instantly start and stop a virtual timer on any record, provided that the table is set up to work with this script. Aside from adding the script into a Scripting block, setup only requires three new fields:

  1. A single line text field, which will store the script’s timing data (used internally; may be hidden)
  2. A duration field, where the script will store the tracked time
  3. A button field, which must be set to trigger the Scripting block that contains the script. I suggest labeling this button “Start/Stop,” for reasons that you’ll see below.

Use: Click the button once to start the “timer.” Click it again to stop it and see the duration. Need to continue timing on that same record? Click the button again to restart. The next click after that will tally the elapsed time from both timing “sessions” and put the total in the duration field. Start and stop the timer as much as you need.

How it works: Each time you click the button, the script adds the current time (converted to a number) to an array, and stores that array in the single line text field. An time that a button click results in an even number of values in the array, the script calculates the difference between each pair of values, and stores the resulting duration in the duration field.

The benefit of this timer system is that you can have multiple records all “timing” simultaneously, and all using the same Scripting block. Using Airtable’s timer block, you first have to pick a record for the timer, and you need one timer block per record that you wish to time.

One optional feature in the script is to clear the duration field while the timer is “active.” This is one possible way to avoid confusion when looking at a record and wondering whether the timer is active or not. :slightly_smiling_face: It’s not exactly a status indicator, but it’s a first start. I’ve got some other ideas that I plan on adding to later updates, including optional status indicators (text/emojis), and an alternate method of starting/stopping the timer using automation so that the blocks sidebar doesn’t need to be involved.

Here’s the script.

32 Replies 32

The exact same script won’t work. To make it work in an automation context requires some changes to the script, and and as I said, it actually takes two scripts: one to start the timer, the other to stop it.

The automation knows exactly which record triggered it. That info has to be passed from the trigger to the script using the input mechanism designed for script actions.

As I said above, I’ve got plans to share the details on how to build this out, but it’s going to take a while to outline it all. I might be able to get to it sometime during this holiday break, but I can’t make any promises.

SamA
5 - Automation Enthusiast
5 - Automation Enthusiast

Thank you so much!! This is fantastic, is there an easy way to keep a timestamp (with the date) in the json array as well? So we can do other calculations such as monthly time tracked for a record?

@SamA Each entry in the the JSON array is already a timestamp. It’s just represented as the number of milliseconds since January 1, 1970 (created using the getTime() method on a Date object). If you want to use these values in a script of your own, make a new Date object, then use the setTime() method to set that object using a number from the array. Each pair of numbers represents a start and stop time. Get the difference in each pair, add them all up, and you’ve got the total elapsed time. To isolate totals by month would take a little more work, but it’s doable.

Thank you for your reply Justin, I’ve created an object that holds a few other values (category and current user) in there as well…

Vykintas_Gloden
6 - Interface Innovator
6 - Interface Innovator

Works perfectly. Thank you!

Justin, Do you have the two scripts mentioned above you can share for creating this via an automation?

Nicole,

Here is what I’ve done for my automation to work.

// ------------------------ START CONFIGURATION ------------------------//

let dataField = "Timer Data";
let durationField = "Duration";
let clearWhileTiming = true;
let inputConfig = input.config();
let recordID = inputConfig.recordID;

// ------------------------- END CONFIGURATION -------------------------//

// Get the record
let mainTable = base.getTable("yourTable"); // Load the main table - change "yourTable" according to your base
let mainQueryResult = await mainTable.selectRecordsAsync(); // Get all records in the mainTable
let record = mainQueryResult.getRecord(`${inputConfig.recordID}`); // Find the updated record by using the ID from the input
let timerData = record.getCellValue(dataField);
let values = timerData === null ? [] : JSON.parse(timerData);
let toStore = {};

And on line 74, change table to mainTable

@Nicole_Merwin ,

Did you add input variables on left hand side?

image

Sorry, @Nicole_Merwin , but I never got back to the automation version. I might revisit it eventually, but it’s impossible to say when that will be.

Operations_ACM
5 - Automation Enthusiast
5 - Automation Enthusiast

@Justin_Barrett firstly this script is amazing. I was wondering if you had looked at this again? I am currently building out in interfaces and having this run via automation with a button trigger would be a game changer.