【问题标题】:jquery define timeout when calling functionjquery在调用函数时定义超时
【发布时间】:2017-06-25 05:22:51
【问题描述】:

我坚持让超时工作。我已经有一个工作代码,但在我看来这是错误的方法。

工作代码,但可能不是最好的:

/* Autosave */
// On load we hide all autosave messages.
$('.jform_params_autosave-cg').hide();
// Below is the function that handles the autosave.
$.fn.autoSave = function(){
  // We remove the autosave message from it's place defined by the xml and add it to the system message container.
  var autosavemessage = $('.jform_params_autosave-cg');
  autosavemessage.detach();
  autosavemessage.appendTo('#system-message-container');
  // Now we show the message.
  $('.jform_params_autosave-cg').show();
  // Here we save the extension.
  Joomla.submitbutton('module.apply');
}
// On change of the below elements we run the autosave.
//------------------------------------------//
// DUPLICATE AUTOSAVE FUNCTION BELOW
//------------------------------------------//
// Autosave: Theme Selection
$("#jform_params_theme_selection").change(function() {
  $.fn.autoSave();
});
// Autosave: Add Content
$("a.group-add.btn.btn-mini.button.btn-success").click(function() {
  setTimeout(
    function()
    {
      $.fn.autoSave();
    }, 5000);
});

功能:

$.fn.autoSave = function(){
  // We remove the autosave message from it's place defined by the xml and add it to the system message container.
  var autosavemessage = $('.jform_params_autosave-cg');
  autosavemessage.detach();
  autosavemessage.appendTo('#system-message-container');
  // Now we show the message.
  $('.jform_params_autosave-cg').show();
  // Here we save the extension.
  Joomla.submitbutton('module.apply');
}

函数调用

$("#jform_params_theme_selection").change(function() {
  $.fn.autoSave();
});

有超时的函数调用

$("a.group-add.btn.btn-mini.button.btn-success").click(function() {
  setTimeout(
    function()
    {
      $.fn.autoSave();
    }, 5000);
});

我想达到什么目的

  1. 在函数内部设置超时。
  2. 定义调用函数时的超时时间。

定义我的意思是称它为$.fn.autoSave(5000);$.fn.autoSave().timeout(500);之类的东西

我一直在尝试获得有效的代码,但到目前为止还没有成功。每当我获得更多成功或添加细节时,都会不断更新这篇文章。

感谢大家的帮助。

任何指向现有 SO 问题的链接也将不胜感激,因为我可能会在谷歌上搜索错误的关键词。

【问题讨论】:

  • 您正在寻找“创建插件”检查这里learn.jquery.com/plugins/basic-plugin-creation
  • @TolgahanAlbayrak 谢谢你的链接。确实,这就是我正在寻找的。不幸的是,在我接管页面的快速浏览中,没有超时的示例。打算从头到尾阅读它,但不要认为它在页面上。

标签: javascript jquery function joomla timeout


【解决方案1】:

这里是你的函数的修改版本。现在它有可选的超时参数。你可以像这样使用它

$('selector').autoSave(5000)$('selector').autoSave()

$.fn.autoSave = function(timeout) {
  function doIt() {
    // We remove the autosave message from it's place defined by the xml and add it to the system message container.
    var autosavemessage = $('.jform_params_autosave-cg');
    autosavemessage.detach();
    autosavemessage.appendTo('#system-message-container');
    // Now we show the message.
    $('.jform_params_autosave-cg').show();
    // Here we save the extension.
    Joomla.submitbutton('module.apply');
    return this;
  }
  timeout = Number(timeout) || 0;
  var f = doIt.bind(this);
  if(timeout < 0) return f();
  setTimeout(f, timeout);
  return this;
}

【讨论】:

  • 非常感谢它可以直接使用。你介意解释一下最后 5 行在做什么吗? PS:在这种情况下,选择器是:$.fn.autoSave(5000);
  • 当然,它将当前逻辑包装到内部回调中,以摆脱两次编写相同的行。然后它检查超时参数是否为正数。如果不是直接调用主逻辑,否则触发我们的内部回调超时。
  • @purple11111 实际上$.fn.autoSave 不是选择器。它直接调用插件方法。当您在 $.fn 对象中有一个函数时,这意味着您将能够使用与 $('your_element_selector').yourFunction() 相同的函数。它是 jQuery 的插件功能
  • 啊好吧。我相信在阅读了您从 A 到 Z 提供的链接后,我会理解。谢谢您,您在这里真的帮了我很多忙!使用您的代码,它看起来比复制超时要好得多!祝你有美好的一天!
猜你喜欢
  • 2016-09-24
  • 1970-01-01
  • 2019-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-24
  • 2014-05-02
相关资源
最近更新 更多