Skip to content

An angular services that acts as a factory for creating testable timer objects

License

Notifications You must be signed in to change notification settings

mike-hanson/angular-timers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

angular-timers

An angular services that acts as a factory for creating testable objects that defer or repeat execution of functions. Syntax and usage is very close to the standard JavaScript setTimeout and setInterval functions, which it wraps. As objects they can be mocked and fully controlled for testing.

Installation

You can of course download the source code and use the files from the dist folder, but the best way to install the package is with bower using

    bower install angular-timers --save

Usage

Obviously you will need to include the script in your web page somewhere after angular has been loaded

	<script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js'></script>
	<script src='/bower_components/angular-timers/angular-timers.min.js'></script>
	<script src='/js/app.js'></script>

Next you will need to import the module into your angular application or module

// app.js
angular.module('app', ['angular-timers']);

And then inject the service into a controller, directive or service

// module.js

angular.module('app')
	.controller('MyController', ['timerService', function(timerService){
	// following examples go here
}]

DelayTimer

To execute a function once after a half a second

	var timer = timerService.createDelayTimer(500) // delay is specified in milliseconds;
	timer.start(function (){
		console.log('Here I am');
	});

To cancel the above timer before the delay expires

	var cancelled = timer.cancel(); //tells you if it actually stopped the function in time

To query the above timer delay

	var delay = timer.getDelay(); // returns 500

RepeatingTimer

To execute a function repeatedly every half second

	var timer = timerService.createRepeatingTimer(500) // interval is specified in milliseconds;
	timer.start(function (){
		console.log('Here I am');
	});

To stop the above timer from executing the function again

	timer.stop();

To query the above timer interval

	var interval = timer.getInterval(); // returns 500

That's it for now. I created this for my own use and may make changes as I use it more, once I feel it is fully stable I will try add some some API documentation and samples for usage and testing.

About

An angular services that acts as a factory for creating testable timer objects

Resources

License

Stars

Watchers

Forks

Packages

No packages published