SetTimeout

From NSB App Studio
Revision as of 12:07, 20 September 2017 by Ghenne (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

timerRef = SetTimeout(function, milliseconds)

ClearTimeout(timerRef)

timerRef = SetInterval(function, milliseconds)

ClearInterval(timerRef)

Description

SetTimeOut is used to schedule an event for a future time, where function is the name of the Sub or Function to be called, and milliseconds is how long in the future it is to be called. Execution does not halt at this statement – it continues with the next statement. It returns a reference to the timeout action which can be used in ClearTimeout.

SetInterval works like SetTimeout, except function is called repeatedly.

In the example below, nextAction is a reference to the nextAction() function. It will evaluate to that function, so it gets called when the timer expires.

nextAction() (with the parens) calls the nextAction() function immediately and would send whatever the function returns into the future.

Try this: in the Chrome Debugger, type nextAction. It will return the text of the function. But if you type nextAction(), it will actually call the function.

Example

Rem SetTimeout statement
T=SetTimeout(nextAction,3000)
 
Function nextAction()
  MsgBox "nextAction"
End Function

Do an interval

Dim timerRef, i
i = 0
timerRef = SetInterval(nextAction, 1000)

Function nextAction()
  MsgBox "nextAction " & i
  i = i + 1
  If i > 3 Then ClearInterval(timerRef)
End Function

Output

(nextAction appears 3 seconds later)

Related Items

ClearInterval, ClearTimeOut, SetInterval