【问题标题】:Passing a var from one Extended function to another将 var 从一个扩展函数传递到另一个
【发布时间】:2011-04-19 11:40:03
【问题描述】:

我在传递 $.PhotoUpdater.doUpdate(url) 的 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}");

【问题讨论】:

    标签: javascript jquery function extend


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      相关资源
      最近更新 更多