日期:2014-05-16  浏览次数:20272 次

我想做一个每秒钟弹窗一次。
window.onload = function(){
  var 弹窗 = new 方法("Hello World!");
  弹窗.alert();

  }

  function 方法(msg) {

  this.msg = msg;

  this.alert = function () {
  alert(this.msg);
  setTimeout("this.alert()",1000); }

   
   
  }
大家看一下我哪里错了。

------解决方案--------------------
HTML code

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">

function Messager(msg) {
    this.msg = msg;
}

Messager.prototype.alert = function() {
    
    alert(this.msg);
    
    var fn = null;
    if (this.alert.bind) {
        fn = this.alert.bind(this);
    } else {
        var targetThis = this;
        fn = function() {
            targetThis.alert.apply(targetThis, arguments);
        };
    }
    setTimeout(fn, 1000);
};


window.onload = function() {
    
    new Messager("Hello").alert();
    
}

</script>
</head>
<body>

</body>
</html>

------解决方案--------------------
改一行代码:
setTimeout("window.onload()",1000);