2

I have written an extension which requires the page to fully load in order to work properly. The function myLoop() is the one which should be fired only after the page fully loads and I also want to add a 5 second delay.

Here is my code :

function like() {
    step_delay_msec = step_delay * 1000;
    i = 0;
    console.log("Hello " + i);
    myLoop();
 var stepAll = setInterval(function () {
     i = 0;
    console.log("Hello " + i);
    myLoop();

}, step_delay_msec);
} 

Note : window.onload won't work because the full page loads after the window has been loaded. The elements load later.

Sanidhay
  • 159
  • 12

3 Answers3

2

Use window.onload() which executes after the page is loaded

window.onload = function() {
    myLoop();
};

Refer this: window.onload vs document.onload

Community
  • 1
  • 1
Vegeta
  • 1,319
  • 7
  • 17
2

You can simply call function on window.onload and set interval on this function.

window.onload = function () { myLoop(); }

function myLoop(){
    setInterval(like(), step_delay_msec);
}

Try this.

Ghanshyam Katriya
  • 1,071
  • 1
  • 11
  • 37
  • Actually the window finish loading before the whole thing actually loads. I still can't get through – Sanidhay Jan 04 '16 at 11:38
1

With chrome extension in your manifest you can write:

"content_scripts": [
  {
  "run_at": "document_end",  // after document loaded
  "js": ["content_script.js"]
  }],

In this way content_script.js will start when the document is loaded.

gaetanoM
  • 41,594
  • 6
  • 42
  • 61