1

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

$(document).ready(function() {

  check();

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

});

我看到这个问题存在一个非常老的问题,但我不明白如何为我的函数使用答案。 StackOverflow 对另一个问题的回答

我想从链接中查看我的功能和建议的解决方案的示例。

我的示例无法正常工作:

//= 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();
  });

});
4

1 回答 1

0

这是根据您粘贴的答案重新编写的。

$(document).ready(check);

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

// In the test file
TestFile.prototype.testDocumentReadyContents = function () {
  check();
}

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

于 2018-08-21T11:20:05.087 回答