Path: blob/main/projects/HexGL/bkcore.coffee/Timer.coffee
4626 views
###1new Date().getTime() wrapper to use as timer.23@class bkcore.Timer4@author Thibaut 'BKcore' Despoulain <http://bkcore.com>5###6class Timer78###9Creates a new timer, inactive by default.10Call Timer.start() to activate.11###12constructor: ()->1314@time =15start: 016current: 017previous: 018elapsed: 019delta: 02021@active = false2223###24Starts/restarts the timer.25###26start: ()->2728now = (new Date).getTime()2930@time.start = now31@time.current = now32@time.previous = now33@time.elapsed = 034@time.delta = 03536@active = true3738###39Pauses(true)/Unpauses(false) the timer.4041@param bool Do pause42###43pause: (doPause)->4445@active = not doPause4647###48Update method to be called inside a RAF loop49###50update: ()->5152if not @active then return5354now = (new Date).getTime()5556@time.current = now57@time.elapsed = @time.current - @time.start58@time.delta = now - @time.previous59@time.previous = now6061###62Returns a formatted version of the current elapsed time using msToTime().63###64getElapsedTime: ()->6566return @constructor.msToTime(@time.elapsed)6768###69Formats a millisecond integer into a h/m/s/ms object7071@param x int In milliseconds72@return Object{h,m,s,ms}73###74@msToTime: (t)->7576ms = t%100077s = Math.floor((t/1000)%60)78m = Math.floor((t/60000)%60)79h = Math.floor(t/3600000)8081return {h:h, m:m, s:s, ms,ms}8283###84Formats a millisecond integer into a h/m/s/ms object with prefix zeros8586@param x int In milliseconds87@return Object<string>{h,m,s,ms}88###89@msToTimeString: (t)->9091time = @msToTime(t)9293time.h = @zfill(time.h, 2)94time.m = @zfill(time.m, 2)95time.s = @zfill(time.s, 2)96time.ms = @zfill(time.ms, 4)9798return time99100###101Convert given integer to string and fill with heading zeros102103@param num int Number to convert/fill104@param size int Desired string size105###106@zfill: (num, size)->107108len = size - num.toString().length109return if len > 0 then new Array(len+1).join('0') + num else num.toString()110111###112Exports113@package bkcore114###115exports = exports ? @116exports.bkcore ||= {}117exports.bkcore.Timer = Timer118119