user6681863 user6681863 - 4 months ago 7
jQuery Question

Can we execute the alert message using the following code?

Can we alert the message using the following function ??

var run_databases = function() {
alert('welcome');
};

window.setTimeout(run_databases, 1000);

Answer

You need to name the function or assign it to a variable:

var run_databases = function() {
  alert('welcome');
};

// You need to name this function
function setTimeOutFoo() {

  window.setTimeout(run_databases, 1000);

};

// Or assign the unnamed function to a variable
var setTimeOutVar = function() {

  window.setTimeout(run_databases, 1000);

};

setTimeOutFoo(); // Calling the function
setTimeOutVar();

You can try something like this:

window.onload = function() {
  console.log('Page has finished loading');  
  
  //I am adding a time out so you can see the loading
  // You can remove the "setTimeout(function () {"
  // And you can also remove the "}, 2000);"
  
  setTimeout(function () { // Remove this line
    
    document.getElementById('loading').innerHTML = "Page Loaded";
    
  }, 2000); // Remove this line
  
};
<p id="loading">Page Loading...</p>