My JavaScript is as follows:
var util = require('util'); EventEmitter = require('events').EventEmitter; var Ticker = function() { var self = this; setInterval( function() { self.emit('tick'); }, 1000 ); }
What's the equivalent CoffeeScript?
Definition and UsageThe setInterval() method calls a function at specified intervals (in milliseconds). The setInterval() method continues calling the function until clearInterval() is called, or the window is closed. 1 second = 1000 milliseconds.
“setinterval for 5 times” Code Answer'svar intervalID = setInterval(alert, 1000); // Will alert every second. // clearInterval(intervalID); // Will clear the timer. setTimeout(alert, 1000); // Will alert once, after a second.
Try doing: setInterval(function () { for (var i = 0; i < 1000; i++) { // YOUR CODE } }, 10); You will actually make your code more efficient by avoiding the callback calling overhead and having longer intervals will make your code run more predictably.
util = require 'util' EventEmitter = require('events').EventEmitter Ticker = -> self = this setInterval -> self.emit 'tick' , 1000 true
You add the second parameter by lining up the comma with the function you are passing to, so it knows a second parameter is coming.
It also returns true instead of setInterval, although I can't personally see the advantage of not returning the setInterval.
Here is a version with thick arrow (see comments), and destructuring assignment (see other comment). Also, returning the setInterval instead of explicitly returning true.
util = require 'util' {EventEmitter} = require 'events' Ticker = -> setInterval => @emit 'tick' , 1000
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With