Skip to content

Commit

Permalink
Merge pull request #1518 from platinumazure/close-over-settimeout
Browse files Browse the repository at this point in the history
Avoid issues caused by clobbering of global setTimeout
  • Loading branch information
jrburke committed Mar 28, 2016
2 parents c4b74da + 9d95c7b commit 3b5407e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
4 changes: 2 additions & 2 deletions require.js
Expand Up @@ -8,7 +8,7 @@
/*global window, navigator, document, importScripts, setTimeout, opera */

var requirejs, require, define;
(function (global) {
(function (global, setTimeout) {
var req, s, head, baseElement, dataMain, src,
interactiveScript, currentlyAddingScript, mainScript, subPath,
version = '2.2.0',
Expand Down Expand Up @@ -2139,4 +2139,4 @@ var requirejs, require, define;

//Set up with config info.
req(cfg);
}(this));
}(this, setTimeout));
2 changes: 2 additions & 0 deletions tests/all.js
Expand Up @@ -7,6 +7,8 @@ var hasToString = (function () {

doh.registerUrl("simple", "../simple.html");

doh.registerUrl("setTimeout", "../setTimeout.html");

//PS3 does not like this test
doh.registerUrl("baseUrl", "../baseUrl.html");

Expand Down
25 changes: 25 additions & 0 deletions tests/setTimeout-tests.js
@@ -0,0 +1,25 @@
(function (global) {
// Store local setTimeout reference for monkey-patching DOH
var localSetTimeout = global.setTimeout;

// Clobber setTimeout to ensure that the below require() still works...
global.setTimeout = null;

// ...but ensure doh.setTimeout does not get broken
doh.setTimeout = function (fn, delay) {
return localSetTimeout.call(global, fn, delay);
};

require({ baseUrl: "./" }, ["simple"], function(simple) {
doh.register(
"setTimeout",
[
function checkSetTimeout(t){
t.is("blue", simple.color);
}
]
);

doh.run();
});
})(this);
14 changes: 14 additions & 0 deletions tests/setTimeout.html
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<title>require.js: setTimeout Test</title>
<script type="text/javascript" src="../require.js"></script>
<script type="text/javascript" src="doh/runner.js"></script>
<script type="text/javascript" src="doh/_browserRunner.js"></script>
<script type="text/javascript" src="setTimeout-tests.js"></script>
</head>
<body>
<h1>require.js: setTimeout Test</h1>
<p>Check console for messages</p>
</body>
</html>

0 comments on commit 3b5407e

Please sign in to comment.