Lodash - timing

About

These functions provide utilities for performing timed actions.

API

.clearInterval()

.clearInterval(interval : Object)

Cancels a repeating task previously set with _.setInterval(). If interval was not created by _.setInterval(), this function does nothing.


.clearTimeout()

.clearTimeout(timeout : Object)

Cancels a timeout previously set with _.setTimeout(). If timeout was not created by _.setTimeout(), this function does nothing.


.performance()

.performance(func : Callable; count : Integer; thisObj : Object; parameters : Collection)

A convenience function that creates a Performance instance with the given values and calls its .alert() function.

count is pinned to a minimum of 1.


.setInterval()

.setInterval(func : Callable; delay : Integer { ; params : Collection }) : Object

Repeatedly calls a function with a fixed time delay between each call.

func is the Callable to execute, which will be called immediately via .apply(), and then repeatedly once delay milliseconds have elapsed. The elements of params will be passed as positional parameters to func.

The actual delay between calls may be slightly longer than delay due to any inaccuracies in 4D’s internal timer.

The function returns a shared interval object that can be used to cancel the interval via _.clearInterval().

NOTE

This function creates a new process that implements the interval. Therefore any non-shared objects or collections you pass to this function, or that are referenced within func, will be copied. If you want to retain a reference to the original, you must pass a shared object or collection.

Example

Here is a simple example that will display a progress message with a user’s name and the current time every second, then change the name after 3 seconds.

Function intervalTest()
  $progress:=Progress New()
  $formula:=Formula(Progress SET TITLE($progress; _.format(\
    "${1} ${2.name}, the time is ${3|time}"; \
    $1; \
    $2; \
    Current time\
    )))

  // NOTE: For the code below to work with setInterval,
  // `This` must be a shared object.

  // "Hello" is $1 in the formula above, $2 is `This`
  $params:=New shared collection("Hello"; This)
  $interval:=_.setInterval($func; 1000; $params)

  // Wait 3 seconds and change the name
  DELAY PROCESS(Current process; 3*60)

  Use (This)
    This.name:="Mike"
  End use

  // Wait another 3 seconds, then stop the interval
  DELAY PROCESS(Current process; 3*60)
  _.clearInterval($interval)
  Progress QUIT($progress)

.setTimeout()

.setTimeout(func : Callable; delay : Integer { ; params : Collection }) : Object

Sets a timer which executes a function asynchronously once the timer expires.

func is the Callable to execute, which will be called via .apply() once delay milliseconds have elapsed. The elements of params will be passed as positional parameters to func.

The actual delay will be a few milliseconds longer than delay due to the time it takes to set up the timer and any inaccuracies in 4D’s internal timer.

The function returns a shared timeout object that can be used to prematurely cancel the timer via _.clearTimeout(). Ordinarily it is not necessary to clear the timer, as it will be automatically cleared when the timer expires.

NOTE

This function creates a new process that implements the timer. Therefore any non-shared objects or collections you pass to this function, or that are referenced within func, will be copied. If you want to retain a reference to the original, you must pass a shared object or collection.

Example

Let’s say you want to give a user one minute to fill out a form, and log them out if it is not submitted before that time. You might have a User class like this:

Function fillOutForm()
  $params:=New shared collection(\
    This; \
    "Sorry, but you have been logged out due to inactivity."\
    )

  // $1 is `This`, $2 is the message
  This.formTimeout:=_.setTimeout(Formula($1.formExpired($2)); 60*1000; $params)

  // Do whatever to present the form to the user


Function formExpired($message : Text)
  // Do whatever to display $message to the user

  This.logout()


Function submitForm()
  // Make sure they are not logged out by the timer
  _.clearTimeout(This.formTimeout)

  // Do whatever to process the form
Last Updated:
Contributors: Aparajita