Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Synchronet
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Main
Synchronet
Commits
4a34cffc
Commit
4a34cffc
authored
13 years ago
by
mcmlxxix
Browse files
Options
Downloads
Patches
Plain Diff
timer that triggers events
parent
62af08df
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
exec/load/event-timer.js
+84
-0
84 additions, 0 deletions
exec/load/event-timer.js
with
84 additions
and
0 deletions
exec/load/event-timer.js
0 → 100644
+
84
−
0
View file @
4a34cffc
/*
Event Timer - for Synchronet 3.15a+ (2011)
- code by mcmlxxix
methods:
- Timer.cycle()
- Timer.addEvent()
sample usage:
load(event-timer.js);
var timer=new Timer();
function event() {
print("hello\r\n");
}
timer.addEvent(10,10,event);
//will print "hello" 10 times at 10 second intervals
while(timer.events.length > 0) {
timer.cycle();
mswait(1000);
}
//script will end when the event runs its course
*/
function
Timer
()
{
this
.
events
=
[];
/* called by parent script, generally in a loop, and generally with a pause or timeout to minimize cpu usage */
this
.
cycle
=
function
()
{
var
now
=
time
();
var
count
=
0
;
/* scan all events and run any that are due */
for
(
var
e
=
0
;
e
<
this
.
events
.
length
;
e
++
)
{
var
event
=
this
.
events
[
e
];
if
(
now
-
event
.
lastrun
>=
event
.
interval
)
{
/* run event */
event
.
run
();
count
++
;
/* an event with a repeat set to TRUE will never expire */
if
(
event
.
repeat
===
true
)
continue
;
/* decrement event repeat counter */
if
(
event
.
repeat
>
0
)
event
.
repeat
--
;
/* if event has expired, or is set to run only once, delete it */
if
(
!
event
.
repeat
)
this
.
events
.
splice
(
e
--
,
1
);
}
}
/* return the number of events run this cycle */
return
count
;
}
/* create a new event, do not include () on action parameter */
this
.
addEvent
=
function
(
interval
,
repeat
,
action
)
{
var
event
=
new
Event
(
interval
,
repeat
,
action
);
this
.
events
.
push
(
event
);
}
/* event object created by addEvent */
function
Event
(
interval
,
repeat
,
action
)
{
/* last time_t at which event was executed */
this
.
lastrun
=
time
();
/* seconds between event occurance */
this
.
interval
=
interval
;
/* number of times to repeat, true to repeat indefinitely, false to run only once */
this
.
repeat
=
repeat
;
/* function called when event is run */
this
.
action
=
action
;
/* run event */
this
.
run
=
function
()
{
this
.
action
();
this
.
lastrun
=
time
();
}
}
};
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment