如何调用$(Document).Ready(Function(){…});来自另一个文件JS [英] How to call $(document).ready(function() {…}); from another file JS

查看:21
本文介绍了如何调用$(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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆