Advertisement
Guest User

Untitled

a guest
Apr 17th, 2014
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. var test = function(spec) {
  2. spec.timers = spec.timers || {};
  3. var that = {};
  4.  
  5. var addTimers = function(name, seconds, func) {
  6. spec.timers[name] = {"seconds":seconds, "func":func};
  7. };
  8. that.addTimers = addTimers;
  9.  
  10. var startTimers = function() {
  11. var name, row;
  12. for(name in spec.timers) {
  13. row = spec.timers[name];
  14. row.timer = setTimeout(row.func, row.seconds*1000, name);
  15. }
  16. };
  17. that.startTimers = startTimers;
  18.  
  19. return that;
  20. };
  21.  
  22. var say = function(msg) {
  23. console.log(msg);
  24. };
  25.  
  26. var my = test({});
  27. my.addTimers('bob', 5, say);
  28. my.startTimers();
  29.  
  30. describe("timers", function() {
  31. var t, timerCallback;
  32.  
  33. beforeEach(function() {
  34. t = test({});
  35. timerCallback = jasmine.createSpy("timerCallback");
  36. jasmine.clock().install();
  37. };
  38.  
  39. afterEach(function() {
  40. jasmine.clock().uninstall();
  41. };
  42.  
  43. it("should start all queued timers", function() {
  44. t.addTimers('bob', 100, timerCallback);
  45. t.startTimers();
  46. expect(timerCallback).not.toHaveBeenCalled();
  47. jasmine.clock().tick(101*1000);
  48. expect(timerCallback).toHaveBeenCalled();
  49. });
  50.  
  51. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement