如何从另一个文件 JS 调用 $(document).ready(function() {...});



我想知道如何在不从另一个js文件重构的情况下调用下面的函数。

$(document).ready(function() {
check();
function check () {
setTimeout(function(){
location.reload(true);
}, 10000);
}
});

我看到这个问题存在一个很老的问题,但我不明白如何将答案用于我的函数。 堆栈溢出 另一个问题的答案

我想从链接中看到我的函数和建议的解决方案的示例。

我的示例无法正常工作:

//= require rspec_helper
//= require background_index
//= require sinon

describe("Background Index, timeout after 10s", function() {
// Tweak for testing
var doc_ready = $.readyList[0]();
it ("Reload the location", function(){
clock = sinon.useFakeTimers();
var check = doc_ready.check();
var set_timeout = doc_ready.setTimeout();
/*var stub_check = sinon.stub(check, "check");
var stub_timeout = sinon.stub(timeout, "setTimeout");*/

timedOut = false;

setTimeout(function () {
timedOut = true;
}, 1000);

timedOut.should.be.false;
clock.tick(10010);
timedOut.should.be.true;
clock.restore();
});
});

这是从您粘贴的答案重写的。

$(document).ready(check);
function check () {
setTimeout(function(){
location.reload(true);
}, 10000);
}   
// In the test file
TestFile.prototype.testDocumentReadyContents = function () {
check();
}

更好的方法是将所有JS文件包含在<script src="./path-to-file">HTML中,然后按照您希望调用它们的顺序调用函数。

最新更新