timer.js 855 B

1234567891011121314151617181920212223242526272829
  1. // Copyright (C) 2017 Ecma International. All rights reserved.
  2. // This code is governed by the BSD license found in the LICENSE file.
  3. /*---
  4. description: |
  5. Used in website/scripts/sth.js
  6. ---*/
  7. //setTimeout is not available, hence this script was loaded
  8. if (Promise === undefined && this.setTimeout === undefined) {
  9. if(/\$DONE()/.test(code))
  10. $ERROR("Async test capability is not supported in your test environment");
  11. }
  12. if (Promise !== undefined && this.setTimeout === undefined) {
  13. (function(that) {
  14. that.setTimeout = function(callback, delay) {
  15. var p = Promise.resolve();
  16. var start = Date.now();
  17. var end = start + delay;
  18. function check(){
  19. var timeLeft = end - Date.now();
  20. if(timeLeft > 0)
  21. p.then(check);
  22. else
  23. callback();
  24. }
  25. p.then(check);
  26. }
  27. })(this);
  28. }