Timer

Timers enable your automation scripts to perform tasks over a period of time, or after a specified delay. You create a Timer object using one of these functions:

The only instance method available for use with a Timer object is a method for stopping a repeating timer:

This method is called within the repeating timer’s function, after a specified condition is met.

The single property of a Timer object is its assigned time value:

Let’s examine the two types of Timer objects, delayed and repeating.

Delayed Timer

A delayed Timer is useful for situations where you want a script to execute after a specified time period. To create a delayed timer, call the once handler on the Timer class and provide two items: 1) the number of seconds the timer should wait before executing 2) a function containing a passed referenced to the created timer object, and the function code to execute.

In the following example, the script will log the start time (in milliseconds) and the time when the passed function is executed:

// delay a specified amount start = new Date().getTime() console.log(start) Timer.once(5,function(timer){ now = new Date().getTime() console.log(now) })
omnigraffle:///omnijs-run?script=start%20%3D%20new%20Date%28%29%2EgetTime%28%29%0Aconsole%2Elog%28start%29%0ATimer%2Eonce%285%2Cfunction%28timer%29%7B%0A%09now%20%3D%20new%20Date%28%29%2EgetTime%28%29%0A%09console%2Elog%28now%29%0A%7D%29

Repeating Timer: Stop after Duration

Repeating timers are designed to stop after a condition is met. In this first example of a repeating timer, the condition is that a specified amount of time has passed. This example uses the standard JavaScript getTime() method on the Date class to get the current time (in milliseconds) to determine if a specified number of seconds (in this case 20000 milliseconds or 20 seconds) has passed. If it has, then the function is halted by calling the cancel() method on the passed timer object. Note that a time value of one-second (line 4) is passed to the function as the interval between function repetitions.

// repeat function after interval, stopping after duration start = new Date().getTime() console.log(start) Timer.repeating(1,function(timer){ now = new Date().getTime() console.log(now) console.log(now - start) if (now - start > 20000){ console.log('done') timer.cancel() } })
omnigraffle:///omnijs-run?script=start%20%3D%20new%20Date%28%29%2EgetTime%28%29%0Aconsole%2Elog%28start%29%0ATimer%2Erepeating%281%2Cfunction%28timer%29%7B%0A%09now%20%3D%20new%20Date%28%29%2EgetTime%28%29%0A%09console%2Elog%28now%29%0A%09console%2Elog%28now%20-%20start%29%0A%09if%20%28now%20-%20start%20%3E%2020000%29%7B%0A%09%09console%2Elog%28%27done%27%29%0A%09%09timer%2Ecancel%28%29%0A%09%7D%0A%7D%29

Repeating Timer: Stop after Specified Repetition

The condition for stopping the following example of a repeating timer is that a specified number of repetitions have occurred. Note that the time value for the delay between repetitions is five seconds (line 6).

// repeat function after interval x times start = new Date().getTime() console.log(start) var counter = 0 var repeats = 5 Timer.repeating(5,function(timer){ now = new Date().getTime() console.log(now) console.log(now - start) if (counter == repeats){ console.log('done') timer.cancel() } else { counter = counter + 1 } })
omnigraffle:///omnijs-run?script=start%20%3D%20new%20Date%28%29%2EgetTime%28%29%0Aconsole%2Elog%28start%29%0Avar%20counter%20%3D%200%0Avar%20repeats%20%3D%205%0ATimer%2Erepeating%285%2Cfunction%28timerObj%29%7B%0A%09now%20%3D%20new%20Date%28%29%2EgetTime%28%29%0A%09console%2Elog%28now%29%0A%09console%2Elog%28now%20-%20start%29%0A%09if%20%28counter%20%3D%3D%20repeats%29%7B%0A%09%09console%2Elog%28%27done%27%29%0A%09%09timerObj%2Ecancel%28%29%0A%09%7D%20else%20%7B%0A%09%09counter%20%3D%20counter%20%2B%201%0A%09%7D%0A%7D%29

Repeating Timer: Stop on Condition

The following example of a repeating timer is an automated race between shapes in an OmniGraffle document. And the condition for stopping the repeating timer is that one of the shapes will reach the finish line on the right side of the document. The timer function is set to execute every one-half second (0.5 line 51) and the function relies upon the standard JavaScript methods of floor() and random() of the Math class to determine how many pixels each graphic moves to the right. Fun stuff!

var alert = new Alert("Object Race", "This example script will delete all graphics on the current canvas and resize it.\n\nShould the script continue?") alert.addOption("Continue") alert.addOption("Stop") alert.show(function(result){ if (result != 0){ throw new Error('script cancelled') } else { cnvs = document.windows[0].selection.canvas // delete existing graphics g = cnvs.graphics g = g.reverse() for(i = 0; i < g.length; i++){ g[i].remove() } // resize and center the canvas cnvs.size = new Size(1024,768) cnvs.canvasSizingMode = CanvasSizingMode.Fixed document.windows[0].centerVisiblePoint = cnvs.background.geometry.center // add shapes var diamond = cnvs.newShape() var circle = cnvs.newShape() var square = cnvs.newShape() var finishLine = cnvs.newLine() var startingLine = cnvs.newLine() diamond.textUnitRect = new Rect(0.14, 0.12, 0.75, 0.75) diamond.shape = "Diamond" diamond.geometry = new Rect(32.00, 334.00, 100.00, 100.00) diamond.shadowColor = null diamond.fillColor = Color.RGB(1.0, 0.75, 0.75) circle.textUnitRect = new Rect(0.10, 0.15, 0.80, 0.70) circle.shape = "Circle" circle.geometry = new Rect(32.00, 483.00, 100.00, 100.00) circle.shadowColor = null circle.fillColor = Color.RGB(0.75, 1.0, 0.75) square.shape = "Rectangle" square.geometry = new Rect(32.00, 185.00, 100.00, 100.00) square.shadowColor = null square.fillColor = Color.RGB(0.75, 0.75, 1.0) finishLine.strokeThickness = 8 finishLine.lineType = LineType.Curved finishLine.shadowColor = null finishLine.points = [new Point(886.00, 83.59), new Point(886.00, 684.41)] finishLineHorizontal = 886.00 startingLine.strokeThickness = 8 startingLine.lineType = LineType.Curved startingLine.shadowColor = null startingLine.points = [new Point(168.00, 83.59), new Point(168.00, 684.41)] var factor = 50 Timer.repeating(0.5,function(timer){ //square squareGeometry = square.geometry squareWidth = squareGeometry.width squareOrigin = squareGeometry.origin squareMovement = Math.floor(Math.random() * factor) newSquareHOffset = squareOrigin.x + squareMovement squareOrigin = new Point(newSquareHOffset,squareOrigin.y) squareGeometry.origin = squareOrigin square.geometry = squareGeometry if (newSquareHOffset + squareWidth >= finishLineHorizontal){ finishLine.strokeColor = Color.blue square.fillColor = Color.blue timer.cancel() } // diamond diamondGeometry = diamond.geometry diamondWidth = diamondGeometry.width diamondOrigin = diamondGeometry.origin diamondMovement = Math.floor(Math.random() * factor) newDiamondHOffset = diamondOrigin.x + diamondMovement diamondOrigin = new Point(newDiamondHOffset,diamondOrigin.y) diamondGeometry.origin = diamondOrigin diamond.geometry = diamondGeometry if (newDiamondHOffset + diamondWidth >= finishLineHorizontal){ finishLine.strokeColor = Color.red diamond.fillColor = Color.red timer.cancel() } // circle circleGeometry = circle.geometry circleWidth = circleGeometry.width circleOrigin = circleGeometry.origin circleMovement = Math.floor(Math.random() * factor) newCircleHOffset = circleOrigin.x + circleMovement circleOrigin = new Point(newCircleHOffset,circleOrigin.y) circleGeometry.origin = circleOrigin circle.geometry = circleGeometry if (newCircleHOffset + circleWidth >= finishLineHorizontal){ finishLine.strokeColor = Color.green circle.fillColor = Color.green timer.cancel() } }) } })
omnigraffle:///omnijs-run?script=var%20alert%20%3D%20new%20Alert%28%22Object%20Race%22%2C%20%22This%20example%20script%20will%20delete%20all%20graphics%20on%20the%20current%20canvas%20and%20resize%20it%2E%5Cn%5CnShould%20the%20script%20continue%3F%22%29%0Aalert%2EaddOption%28%22Continue%22%29%0Aalert%2EaddOption%28%22Stop%22%29%0Aalert%2Eshow%28function%28result%29%7B%0A%09if%20%28result%20%21%3D%200%29%7B%0A%09%09throw%20new%20Error%28%27script%20cancelled%27%29%0A%09%7D%20else%20%7B%0A%09%09cnvs%20%3D%20document%2Ewindows%5B0%5D%2Eselection%2Ecanvas%0A%09%09%2F%2F%20delete%20existing%20graphics%0A%09%09g%20%3D%20cnvs%2Egraphics%0A%09%09g%20%3D%20g%2Ereverse%28%29%0A%09%09for%28i%20%3D%200%3B%20i%20%3C%20g%2Elength%3B%20i%2B%2B%29%7B%0A%09%09%09g%5Bi%5D%2Eremove%28%29%0A%09%09%7D%0A%09%09%2F%2F%20resize%20and%20center%20the%20canvas%0A%09%09cnvs%2Esize%20%3D%20new%20Size%281024%2C768%29%0A%09%09cnvs%2EcanvasSizingMode%20%3D%20CanvasSizingMode%2EFixed%0A%09%09document%2Ewindows%5B0%5D%2EcenterVisiblePoint%20%3D%20cnvs%2Ebackground%2Egeometry%2Ecenter%0A%0A%09%09%2F%2F%20add%20shapes%0A%09%09var%20diamond%20%3D%20cnvs%2EnewShape%28%29%0A%09%09var%20circle%20%3D%20cnvs%2EnewShape%28%29%0A%09%09var%20square%20%3D%20cnvs%2EnewShape%28%29%0A%09%09var%20finishLine%20%3D%20cnvs%2EnewLine%28%29%0A%09%09var%20startingLine%20%3D%20cnvs%2EnewLine%28%29%0A%09%09diamond%2EtextUnitRect%20%3D%20new%20Rect%280%2E14%2C%200%2E12%2C%200%2E75%2C%200%2E75%29%0A%09%09diamond%2Eshape%20%3D%20%22Diamond%22%0A%09%09diamond%2Egeometry%20%3D%20new%20Rect%2832%2E00%2C%20334%2E00%2C%20100%2E00%2C%20100%2E00%29%0A%09%09diamond%2EshadowColor%20%3D%20null%0A%09%09diamond%2EfillColor%20%3D%20Color%2ERGB%281%2E0%2C%200%2E75%2C%200%2E75%29%0A%09%09circle%2EtextUnitRect%20%3D%20new%20Rect%280%2E10%2C%200%2E15%2C%200%2E80%2C%200%2E70%29%0A%09%09circle%2Eshape%20%3D%20%22Circle%22%0A%09%09circle%2Egeometry%20%3D%20new%20Rect%2832%2E00%2C%20483%2E00%2C%20100%2E00%2C%20100%2E00%29%0A%09%09circle%2EshadowColor%20%3D%20null%0A%09%09circle%2EfillColor%20%3D%20Color%2ERGB%280%2E75%2C%201%2E0%2C%200%2E75%29%0A%09%09square%2Eshape%20%3D%20%22Rectangle%22%0A%09%09square%2Egeometry%20%3D%20new%20Rect%2832%2E00%2C%20185%2E00%2C%20100%2E00%2C%20100%2E00%29%0A%09%09square%2EshadowColor%20%3D%20null%0A%09%09square%2EfillColor%20%3D%20Color%2ERGB%280%2E75%2C%200%2E75%2C%201%2E0%29%0A%09%09finishLine%2EstrokeThickness%20%3D%208%0A%09%09finishLine%2ElineType%20%3D%20LineType%2ECurved%0A%09%09finishLine%2EshadowColor%20%3D%20null%0A%09%09finishLine%2Epoints%20%3D%20%5Bnew%20Point%28886%2E00%2C%2083%2E59%29%2C%20new%20Point%28886%2E00%2C%20684%2E41%29%5D%0A%09%09finishLineHorizontal%20%3D%20886%2E00%0A%09%09startingLine%2EstrokeThickness%20%3D%208%0A%09%09startingLine%2ElineType%20%3D%20LineType%2ECurved%0A%09%09startingLine%2EshadowColor%20%3D%20null%0A%09%09startingLine%2Epoints%20%3D%20%5Bnew%20Point%28168%2E00%2C%2083%2E59%29%2C%20new%20Point%28168%2E00%2C%20684%2E41%29%5D%0A%0A%09%09var%20factor%20%3D%2050%0A%09%09Timer%2Erepeating%280%2E5%2Cfunction%28timer%29%7B%0A%09%09%09%2F%2Fsquare%0A%09%09%09squareGeometry%20%3D%20square%2Egeometry%0A%09%09%09squareWidth%20%3D%20squareGeometry%2Ewidth%0A%09%09%09squareOrigin%20%3D%20squareGeometry%2Eorigin%0A%09%09%09squareMovement%20%3D%20Math%2Efloor%28Math%2Erandom%28%29%20*%20factor%29%0A%09%09%09newSquareHOffset%20%3D%20squareOrigin%2Ex%20%2B%20squareMovement%0A%09%09%09squareOrigin%20%3D%20new%20Point%28newSquareHOffset%2CsquareOrigin%2Ey%29%0A%09%09%09squareGeometry%2Eorigin%20%3D%20squareOrigin%0A%09%09%09square%2Egeometry%20%3D%20squareGeometry%0A%09%09%09if%20%28newSquareHOffset%20%2B%20squareWidth%20%3E%3D%20finishLineHorizontal%29%7B%0A%09%09%09%09finishLine%2EstrokeColor%20%3D%20Color%2Eblue%0A%09%09%09%09square%2EfillColor%20%3D%20Color%2Eblue%0A%09%09%09%09timer%2Ecancel%28%29%0A%09%09%09%7D%0A%09%09%09%2F%2F%20diamond%0A%09%09%09diamondGeometry%20%3D%20diamond%2Egeometry%0A%09%09%09diamondWidth%20%3D%20diamondGeometry%2Ewidth%0A%09%09%09diamondOrigin%20%3D%20diamondGeometry%2Eorigin%0A%09%09%09diamondMovement%20%3D%20Math%2Efloor%28Math%2Erandom%28%29%20*%20factor%29%0A%09%09%09newDiamondHOffset%20%3D%20diamondOrigin%2Ex%20%2B%20diamondMovement%0A%09%09%09diamondOrigin%20%3D%20new%20Point%28newDiamondHOffset%2CdiamondOrigin%2Ey%29%0A%09%09%09diamondGeometry%2Eorigin%20%3D%20diamondOrigin%0A%09%09%09diamond%2Egeometry%20%3D%20diamondGeometry%0A%09%09%09if%20%28newDiamondHOffset%20%2B%20diamondWidth%20%3E%3D%20finishLineHorizontal%29%7B%0A%09%09%09%09finishLine%2EstrokeColor%20%3D%20Color%2Ered%0A%09%09%09%09diamond%2EfillColor%20%3D%20Color%2Ered%0A%09%09%09%09timer%2Ecancel%28%29%0A%09%09%09%7D%0A%09%09%09%2F%2F%20circle%0A%09%09%09circleGeometry%20%3D%20circle%2Egeometry%0A%09%09%09circleWidth%20%3D%20circleGeometry%2Ewidth%0A%09%09%09circleOrigin%20%3D%20circleGeometry%2Eorigin%0A%09%09%09circleMovement%20%3D%20%20Math%2Efloor%28Math%2Erandom%28%29%20*%20factor%29%0A%09%09%09newCircleHOffset%20%3D%20circleOrigin%2Ex%20%2B%20circleMovement%0A%09%09%09circleOrigin%20%3D%20new%20Point%28newCircleHOffset%2CcircleOrigin%2Ey%29%0A%09%09%09circleGeometry%2Eorigin%20%3D%20circleOrigin%0A%09%09%09circle%2Egeometry%20%3D%20circleGeometry%0A%09%09%09if%20%28newCircleHOffset%20%2B%20circleWidth%20%3E%3D%20finishLineHorizontal%29%7B%0A%09%09%09%09finishLine%2EstrokeColor%20%3D%20Color%2Egreen%0A%09%09%09%09circle%2EfillColor%20%3D%20Color%2Egreen%0A%09%09%09%09timer%2Ecancel%28%29%0A%09%09%09%7D%0A%09%09%7D%29%0A%09%7D%0A%7D%29
UNDER CONSTRUCTION

This webpage is in the process of being developed. Any content may change and may not be accurate or complete at this time.

DISCLAIMER