代码之家  ›  专栏  ›  技术社区  ›  esqew

为什么Javascript不让我关闭我的函数?

  •  1
  • esqew  · 技术社区  · 14 年前

    我不知道伙计们,这是一个非常奇怪的一个,但我可能只是犯了一个简单的错误,甚至没有意识到这一点。

    我是 某种程度上 一个Javascript新手,所以我尝试编写一个脚本,从PHP脚本(只返回一个数字)获取内容,并将数据写入div。。。但是Javascript有其他的想法。我正在Mac OS X上的Chrome上进行测试,尽管它在Safari上也不起作用。

    function getContent() {
     window.setInterval(function () {
      $.get("get.php", function (data) {
       $('#img').slideUp();
       $('#div').html(data);
       $('#div').slideDown();
      }
     }
    }
    

    Uncaught SyntaxError: Unexpected token }
    

    在线 51 ,或行 8

    有人知道为什么会这样失败吗?我不需要关闭我打开的支架吗?

    4 回复  |  直到 14 年前
        1
  •  10
  •   Kobi    14 年前

    你的花括号还可以,但是你少了几个括号:

    function getContent() {
     window.setInterval(function () {
      $.get("get.php", function (data) {
       $('#img').slideUp();
       $('#div').html(data);
       $('#div').slideDown();
      }); //get - end statement
     }, 4000); // setInterval - need another parameter, end statement
    }
    
        2
  •  4
  •   Matthew Flaschen    14 年前

    setInterval .

    function getContent() {
     window.setInterval(function () {
      $.get("get.php", function (data) {
       $('#img').slideUp();
       $('#div').html(data);
       $('#div').slideDown();
      });
     }, 1000);
    }
    
        3
  •  3
  •   naikus    14 年前

    这个window.setInterval文件函数具有以下语法:

    window.setInterval(functionRef, timeout);
    

    就你而言 setInterval $.get() )

    function getContent() {
      // success handler
      var success = function() {
        // declare the function first as "changeUI"
        var changeUI = function() {
          $('#img').slideUp();
          $('#div').html(data);
          $('#div').slideDown();
        };
        // call the setInterval passing the function
        window.setInterval(changeUI, 2000);
      };
    
      // get the resource and call the "success" function on successful response
      $.get("get.php", success);
    }
    
        4
  •  0
  •   Scott M.    14 年前

    你的window.setInterval文件在倒数第二行的}之后缺少a)