我们可以使用以下代码执行警报消息吗?

时间:2016-08-05 09:38:16

标签: javascript jquery

我们可以使用以下函数提醒消息吗?

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

window.setTimeout(run_databases, 1000);

3 个答案:

答案 0 :(得分:1)

不需要其他功能包装(闭包):



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

window.setTimeout(run_databases, 1000);




答案 1 :(得分:0)

您需要命名该函数或将其分配给变量:



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




您可以尝试这样的事情:



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>
&#13;
&#13;
&#13;

向按钮添加工具提示的最简单方法是:

&#13;
&#13;
var btn = document.getElementById("btn");

btn.setAttribute('title', 'This is the tool tip set in JavaScript');
&#13;
<!-- Done with JavaScript -->
<button id="btn">Hover here!</button> 

<!-- Done in the HTML -->
<button id="btn" title="This is tool tip set in HTML">No! Hover here :)</button> 
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您正在使用自调用匿名函数,但语法错误:

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

(function(){
   window.setTimeout(run_databases, 1000);
})();

Working example