简体   繁体   English

如何为这个jquery增量插件编写一个函数?

[英]How do I write a function for this jquery increment plugin?

I have found a nice jquery increment script. 我找到了一个很好的jquery增量脚本。 The increment portion can accept a function or just a regular number. 增量部分可以接受函数或仅接受常规数字。 I am looking to increase at a random number between 1 and 100. How would I write the function for this? 我希望增加1到100之间的随机数。我将如何为此编写函数?

$( document ).ready( function()
{
  $( '.tick' ).ticker(
  {
    incremental : 1,
    delay       : 1500,
    separators  : true
  });
   $(".comma-change").html(".");  
});

<span class="tick">2,354,456</span>

So rather than increase by 1 in the incremental field, I would like to write a function that updates with a random number. 因此,我想写一个用随机数更新的函数,而不是在增量字段中增加1。 With the help of other posts I can write the function that creates a random number no problem: 在其他帖子的帮助下,我可以编写创建随机数的函数没问题:

function random_generator (min, max) {
    return Math.random() * (max - min) + min;
}

but I am having trouble working it into this directly. 但我直接在这方面遇到了麻烦。

Here is the increment portion of the plugin: 这是插件的增量部分:

  Tick = (function() {

    function Tick(element, options) {
      this.element = element;
      if (options == null) options = {};
      this.running = false;
      this.options = {
        delay: options.delay || 1000,
        separators: options.separators != null ? options.separators : false,
        autostart: options.autostart != null ? options.autostart : true
      };
      this.increment = this.build_increment_callback(options.incremental);
      this.value = Number(this.element.html().replace(/[^\d.]/g, ''));
      this.separators = this.element.html().trim().split(/[\d]/i);
      this.element.addClass('tick-active');
      if (this.options.autostart) this.start();
    }

    Tick.prototype.build_increment_callback = function(option) {
      if ((option != null) && {}.toString.call(option) === '[object Function]') {
        return option;
      } else if (typeof option === 'number') {
        return function(val) {
          return val + option;
        };
      } else {
        return function(val) {
          return val + 1;
        };
      }
    };

and then this bit: 然后这一点:

Tick.prototype.tick = function() {
  this.value = this.increment(this.value);
  this.render();
  return this.set_timer();
};

You probably have to update the source of the increment script. 您可能必须更新增量脚本的源。 But if you're lucky, you might be able to use some trickery to do without. 但是,如果你很幸运,你可以使用一些技巧来做。

It's possible that the increment script has something like this in there: 增量脚本可能有这样的东西:

function(options) {
    //...
    currentValue += options.incremental;
    //...
}

If that's the case, you could adjust the value of options.incremental periodically after the fact, like so: 如果是这种情况,您可以在事后定期调整options.incremental的值,如下所示:

var opts;
$( '.tick' ).ticker(opts = {
    incremental : 1,
    delay       : 1500,
    separators  : true
});

setInterval(function () { 
    var min = 1, max = 100; //adjust min/max to suit your needs.
    opts.incremental = Math.random() * (max - min) + min;
}, 500); //any number under 1500 is probably fine

This is a bit of a hack, but it might work. 这有点像黑客,但它可能会奏效。 Might be easier than updating the code of the plugin. 可能比更新插件的代码更容易。 If it doesn't work, you'll probably have to enhance the plugin directly. 如果它不起作用,您可能必须直接增强插件。

All you need to do is translate your function statement into a function expression. 您需要做的就是将函数语句转换为函数表达式。 A function statement defines a name and assigns it a function whereas a function expression is the function itself (an anonymous function). 函数语句定义名称并为其赋值函数,而函数表达式是函数本身(匿名函数)。

You can set the value of a variable to a function like this 您可以将变量的值设置为这样的函数

var random_generator = function(min, max) {
    return Math.random() * (max - min) + min;
};

and then pass the reference to this function (the variable random_generator) as the incremental to your plugin. 然后将对此函数的引用(变量random_generator)作为插件的增量传递。 Or you could inline the function definition like this: 或者您可以像这样内联函数定义:

$( '.tick' ).ticker({
    incremental : function(currentValue) {
                     // currentValue really is the current value of the ticker
                     // this is because the code inside the ticker plugin, that triggers
                     // your function, will take care of passing it as a parameter in
                     return +currentValue + random_generator(1, 100);
                  },
    delay       : 1500,
    separators  : true
});

edit: I didn't realize, that your function has two parameters... that makes the whole thing a little bit more sophisticated. 编辑:我没有意识到,你的函数有两个参数......这使整个事情变得更复杂一些。 You could either hardcode min and max into your function (because the function gets invoked by code from the plugin and you will most definitely not get any arguments), or wrap another anonymous function around it, that provides the two parameters for us. 你可以将min和max硬编码到你的函数中(因为函数是由插件中的代码调用的,你绝对不会得到任何参数),或者在它周围包装另一个匿名函数,它为我们提供了两个参数。

Final edit: I guess you are using this plugin https://github.com/harvesthq/tick ? 最终编辑:我猜你正在使用这个插件https://github.com/harvesthq/tick This plugin can accept a function that will be called every tick and gets passed in the current value of the ticker. 此插件可以接受将在每个tick中调用的函数,并在ticker的当前值中传递。 You have to provide the new value. 您必须提供新值。 I updated the code snippet accordingly. 我相应地更新了代码段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM