SetTimeout: Difference between revisions

From NSB App Studio
Jump to navigation Jump to search
No edit summary
No edit summary
 
(12 intermediate revisions by 3 users not shown)
Line 1: Line 1:
SetTimeOut (''function'', ''milliseconds'')
''timerRef'' =  SetTimeout(''function'', ''milliseconds'')


ClearTimeOut (''settimeout_variable'')
ClearTimeout(''timerRef'')


SetInterval (''function'', ''milliseconds'')
''timerRef'' =  [[SetInterval]](''function'', ''milliseconds'')


ClearInterval(''settimeout_variable'')
ClearInterval(''timerRef'')


== Description ==
== 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.
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 ==
== Example ==
Line 15: Line 23:
<pre>
<pre>
Rem SetTimeout statement
Rem SetTimeout statement
T=setTimeout(nextAction,3000)
T=SetTimeout(nextAction,3000)
   
   
Function nextAction()
Function nextAction()
   msgbox "nextAction"
   MsgBox "nextAction"
end function
End Function
</pre>
</pre>


== Output ==
Do an interval
 
<pre>
<pre>
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
</pre>


== Output ==


nextAction appears 3 seconds later
<pre>
(nextAction appears 3 seconds later)
</pre>
</pre>


Line 36: Line 54:


[[Category:Language Reference]]
[[Category:Language Reference]]
[[Category:Miscellaneous]]

Latest revision as of 12:07, 20 September 2017

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