How to call $(document).ready(function() {…}); from another file JS

霸气de小男生 提交于 2019-12-11 14:05:10

问题


I would like to know how to call the function below without refactoring from another js file.

$(document).ready(function() {

  check();

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

});

I saw a question exist for this issue very old one but I cannot understand how to use the answer for my function. StackOverflow answer from another question

I would like to see an example with my function and the proposed solution from the link.

My example which does not work correctly:

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

});

回答1:


This is re-written from the answer you pasted.

$(document).ready(check);

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

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

A better way would be to include all the JS-files in your HTML with <script src="./path-to-file"> and then just call the functions in the order you want them to be called.



来源:https://stackoverflow.com/questions/51947392/how-to-call-document-readyfunction-from-another-file-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!