将var从一个Extended函数传递到另一个

时间:2010-09-30 17:14:40

标签: javascript jquery function extend

我无法通过$.PhotoUpdater.doUpdate(url)的网址来执行doUpdate功能。

Firefox返回此错误:

useless setTimeout call (missing quotes around argument?)
[Break on this error] timer = setTimeout($.PhotoUpdater.doUpdate(url), 5000) 

我的代码:

$.extend({
  PhotoUpdater: {

    startUpdate: function(organization, gallery){
      url = "/organizations/" + organization + "/media/galleries/" + gallery + "/edit_photo_captions"
      timer = setTimeout($.PhotoUpdater.doUpdate(url), 5000)
    },
    stopUpdate: function(){
      clearTimeout(timer);
      timer = 0;
    },
    doUpdate: function(url){
      $.ajax({type: "GET", url: url, dataType: "script"});
    }
  }
});

我如何称呼它:

$.PhotoUpdater.startUpdate("#{@organization.id}", "#{@gallery.id}");

1 个答案:

答案 0 :(得分:3)

您需要将函数传递给window.setTimeout,而不是调用函数的结果:

startUpdate: function(organization, gallery){
    url = "/organizations/" + organization + "/media/galleries/" + gallery + "/edit_photo_captions";
    timer = window.setTimeout(function() {
        $.PhotoUpdater.doUpdate(url)
    }, 5000);
},