timeout.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.lang.async.timeout"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.lang.async.timeout"] = true;
  8. dojo.provide("dojox.lang.async.timeout");
  9. // Source of Deferred for timeouts
  10. (function(){
  11. var d = dojo, timeout = dojox.lang.async.timeout;
  12. timeout.from = function(ms){
  13. return function(){
  14. var h, cancel = function(){
  15. if(h){
  16. clearTimeout(h);
  17. h = null;
  18. }
  19. },
  20. x = new d.Deferred(cancel);
  21. h = setTimeout(function(){
  22. cancel();
  23. x.callback(ms);
  24. }, ms);
  25. return x;
  26. };
  27. };
  28. timeout.failOn = function(ms){
  29. return function(){
  30. var h, cancel = function(){
  31. if(h){
  32. clearTimeout(h);
  33. h = null;
  34. }
  35. },
  36. x = new d.Deferred(cancel);
  37. h = setTimeout(function(){
  38. cancel();
  39. x.errback(ms);
  40. }, ms);
  41. return x;
  42. };
  43. };
  44. })();
  45. }