Can someone please explain me the construct used in timer_set below?
I know what the code does,
-
It sets the timer which start’s running at the start of the minute.
-
It calls the cb_timer callback. (every 2 secs in this example)
I am having a hard time understanding the timer_set function below.
Kindly explain like you are explaining a 5 year old.
Thanks
firstTime:1b;
timer_set: { [t;f]
.z.ts: { $[firstTime; firstTime::0b; (y[]; .z.ts: y; system "t ", string z)]; }[;f;t]
}
timer_start: {
system "t ", string `int$00:01 - .z.t mod 60000;
}
cb_timer: { [now]
0N! now;
}
timer_set[2000; {cb_timer .z.p;}];
timer_start;
.z.ts function is called every X seconds once the system timer has been set to X
The .z.ts function is getting passed though [;f;t], which will equate to [x:null, y:f, z:t] when inside the function //If you define any function variables, x, y and z will be equal to the first three passed in.
The function performs an If-Else statement with the variable firstTime. If the variable doesn’t exist initially, it sets the variable globally to equal 0B(false in binary).
The next time the function is called the variable will exist and = false, so it will execute the second part of the if-else statement from now on.
This sets system "t " to your input of 2000 cast to a string. Concatenated command:system “t 2000”
Then sets .z.ts equal to the current value of y, which is:(cb_timer .z.p), then executes y (y) to return the current date/time (which will equal .z.ts)
So overall this will print out the current date/time every 2 seconds.
q).z.ts:{ show .z.T}
q)\t 2000
q)12:15:51.049
12:15:53.055
12:15:55.054
12:15:57.055
There’s some generic scheduling code present on kx.com if this helps.
Matt