A newer version of this documentation is available.

View Latest

Function: Simple Timer

    March 23, 2025
    + 12

    Goal: Create a Simple Timer that triggers in the future on each mutation.

    • This function simpleTimer merely demonstrates a basic Evening Timer.

    • Requires Eventing Storage (or metadata collection) and a "source" collection.

    • Will operate on any mutation where the KEY or meta.id starts with "simpletimer:".

    • The result of processing the mutation is a Timer that will be executed approximately 30 seconds in the future.

    • For more details on Timers refer to Timers.

    • For an example of a recurring Timer refer to: Recurring Timer.

    javascript
    // To run configure the settings for this Function, simpleTimer, as follows: // // Version 7.0+ // "Listen to Location" // bulk.data.source // "Eventing Storage" // rr100.eventing.metadata // Binding(s) - none // // Version 6.X // "Source Bucket" // source // "MetaData Bucket" // metadata // Binding(s) - none function TimerCallback(context) { log('From TimerCallback: timer fired with context', context); // do any sort of recurring work here, just update a date_stamp in a doc } function OnUpdate(doc, meta) { // You would typically filter to mutations of interest if (! meta.id.startsWith("simpletimer:")) return; // Make a Date 30 seconds in the future var thirtySecFromNow = new Date(); // Get current time & add 30 sec. to it. thirtySecFromNow.setSeconds(thirtySecFromNow.getSeconds() + 30); // Timers require an id var timer_id = meta.id; // Timers can access a context when they fire var context = {"id": meta.id, "random": Math.random(), "sched": thirtySecFromNow, doc: doc} // crate the Timer createTimer(TimerCallback, thirtySecFromNow, timer_id, context); log("From OnUpdate createTimer(TimerCallback, "+thirtySecFromNow+","+ timer_id+",", context); }