How to call $(document).ready(function() {…}); from another file JS(如何调用$(Document).Ready(Function(){…});来自另一个文件JS)
本文介绍了如何调用$(Document).Ready(Function(){…});来自另一个文件JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如何在不从另一个js文件重构的情况下调用下面的函数。$(document).ready(function() {
check();
function check () {
setTimeout(function(){
location.reload(true);
}, 10000);
}
});
我看到这个问题存在一个非常老的问题,但我不明白如何使用我的函数的答案。 StackOverflow answer from another question
我希望通过链接查看我的函数和建议的解决方案的示例。
我的例子不能正常工作:
//= 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();
}
更好的方法是使用<script src="./path-to-file">
将所有JS文件包含在您的HTML中,然后只需按照您希望它们被调用的顺序调用函数。
这篇关于如何调用$(Document).Ready(Function(){…});来自另一个文件JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:如何调用$(Document).Ready(Function(){…});来自另一个文件JS
基础教程推荐
猜你喜欢
- 响应更改 div 大小保持纵横比 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01
- 动态更新多个选择框 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 在for循环中使用setTimeout 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01