Launch Week Day 5: Introducing Reachability for PHP.Learn More
Socket
Book a DemoSign in
Socket

timeunit

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

timeunit - npm Package Compare versions

Comparing version
1.0.1
to
1.1.0
+1
-1
bower.json
{
"name": "timeunit",
"version": "1.0.1",
"version": "1.1.0",
"description": "Port of Doug Lea's TimeUnit Java class to JavaScript.",

@@ -5,0 +5,0 @@ "main": "lib/timeunit.js",

@@ -1,2 +0,9 @@

* 1.0.1 - Make `sleep()` always go through `setTimeout()`, even if the interval is 0.
* 1.0.0 - Initial release.
## 1.1.0
* Add support for canceling timeouts.
* Add support for intervals.
## 1.0.1
* Make `sleep()` always go through `setTimeout()`, even if the interval is 0.
## 1.0.0 - Initial release.
* Initial release.

@@ -66,3 +66,3 @@ // Generated by CoffeeScript 1.6.3

},
sleep: function(timeout, done) {
sleep: function(timeout, fn) {
var ms;

@@ -73,4 +73,11 @@ if (timeout < 0) {

ms = this.toMillis(timeout);
setTimeout(done, ms);
return null;
return setTimeout(fn, ms);
},
interval: function(interval, fn) {
var ms;
if (interval < 0) {
throw new Error("Invalid interval: " + interval + " " + this.name);
}
ms = this.toMillis(interval);
return setInterval(fn, ms);
}

@@ -106,2 +113,4 @@ };

});
exports.clearTimeout = clearTimeout;
exports.clearInterval = clearInterval;
return exports;

@@ -108,0 +117,0 @@ });

@@ -5,3 +5,3 @@ {

"keywords": ["TimeUnit", "time conversion", "Doug Lea"],
"version": "1.0.1",
"version": "1.1.0",
"author": "Jason Walton (https://github.com/jwalton)",

@@ -8,0 +8,0 @@ "contributors": [

@@ -40,3 +40,4 @@ TimeUnit js

Perhaps even more useful in CoffeeScript, where it is a little easier to use that setTimeout:
Perhaps even more useful in CoffeeScript, where it is a little easier to use that setTimeout, since
it follows the "callback at the end" idiom used by most node.js code:

@@ -43,0 +44,0 @@ timeunit.seconds.sleep 5, () ->

@@ -83,8 +83,18 @@ # Port of [Doug Lea](http://g.oswego.edu/)'s public domain Java TimeUnit class to JavaScript.

# Call `done` after the specified timeout.
sleep: (timeout, done) ->
# Call `fn` after the specified timeout.
#
# Returns a timer ID which can be passed to `timeunit.clearTimeout()` to cancel the timer.
sleep: (timeout, fn) ->
if (timeout < 0) then timeout = 0
ms = @toMillis timeout
setTimeout done, ms
return null
return setTimeout fn, ms
# Call `fn` repeatedly, delaying by `interval` between each execution.
#
# Returns an intervalID which can be passed to `timeunit.clearInterval()` to stop the
# interval.
interval: (interval, fn) ->
if (interval < 0) then throw new Error("Invalid interval: #{interval} #{@name}")
ms = @toMillis interval
return setInterval fn, ms
}

@@ -127,2 +137,5 @@

exports.clearTimeout = clearTimeout
exports.clearInterval = clearInterval
return exports

@@ -12,1 +12,43 @@ timeunit = require '../src/timeunit'

assert.equal 5, timeunit.seconds.convert 5000, timeunit.milliseconds
it "should sleep for 1 second", (done) ->
start = Date.now()
timeunit.seconds.sleep 1, ->
end = Date.now()
slept = timeunit.milliseconds.toSeconds end - start
assert slept > 0.8, "Slept for #{slept} seconds"
assert slept < 1.2, "Slept for #{slept} seconds"
done()
it "should do something 10 times in one second", (done) ->
start = Date.now()
count = 0
intervalId = timeunit.milliseconds.interval 100, ->
count++
timeunit.seconds.sleep 1, ->
timeunit.clearInterval intervalId
assert count > 8, "Happened #{count} times"
assert count < 12, "Happened #{count} times"
done()
it "should not do something if ther interval is cleared", (done) ->
start = Date.now()
count = 0
intervalId = timeunit.milliseconds.interval 100, ->
count++
timeunit.clearInterval intervalId
timeunit.seconds.sleep 1, ->
assert count is 0, "Happened #{count} times"
done()
it "should throw an exception for an invalid interval", ->
err = null
try
intervalId = timeunit.seconds.interval -1, ->
console.log "foo"
timeunit.clearInterval intervalId
catch e
err = e
assert err?, "Expected a failure"

Sorry, the diff of this file is not supported yet