简体   繁体   English

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

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

I would like to know how to call the function below without refactoring from another js file. 我想知道如何在不从另一个js文件重构的情况下调用以下函数。

$(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 来自另一个问题的StackOverflow答案

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

});

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. 更好的方法是使用<script src="./path-to-file">在HTML中包含所有JS文件,然后按照要调用它们的顺序调用这些函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 调用$(document).ready(function(){…}); 从另一个文件 - Calling $(document).ready(function() {…}); from another file 使用$(document).ready(function()访​​问另一个js文件中的函数 - Access function in another js file with $(document).ready(function() 尝试从 jquery $(document).ready 调用外部 js 中定义的 function - Try to call function defined in external js from jquery $(document).ready 如何在HTML中调用js document.ready函数? - How to call js document.ready function in html? 如何在 document.ready 上调用多个 js 函数而不放置它们 jQuery(document).ready(function(){}); - How to call multiple js functions on document.ready without putting them jQuery(document).ready(function(){}); 如何从$(document).ready(function())调用javascript函数 - How to call javascript function from the $(document).ready(function()) 从另一个调用文件的document.ready函数 - Calling document.ready function of file from another 如何从 JS 中的另一个文件调用 function? - How to call a function from another file in JS? 如何通过从html传递变量以调用下面的.js文件来调用document.ready内部的外部.js文件 - how to call external .js which is inside document.ready by passing in variables from html to call the .js file below 如何从 document.ready 调用 jQuery 函数 - How to call jQuery function from document.ready
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM