2012-09-18 42 views
1

我发现了一个不错的jQuery增量脚本。增量部分可以接受函数或只是一个常规数字。我期待在1和100之间的随机数增加。我将如何为此编写函数?如何为这个jquery增量插件编写一个函数?

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

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

因此,而不是增加1个在增量领域,我想编写一个随机数更新的功能。与其他职位的帮助下,我可以写一个产生一个随机数没有问题的功能:

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

,但我有麻烦直接合作成这样。

这里是插件的增量部分:

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; 
     }; 
     } 
    }; 

,然后该位:

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

你需要直接更新增量脚本..你有代码在哪里使用'incremental'? –

+0

是的,代码的增量部分添加到原始问题 – absentx

回答

1

所有你需要做的就是将你的函数语句翻译成一个函数表达式。函数语句定义一个名称并为其分配一个函数,而函数表达式是函数本身(一个匿名函数)。

您可以将变量的值设置为这样的函数

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

,然后随着增量的参照本功能(可变random_generator)传递给你的插件。或者你也可以内联函数的定义是这样的:

$('.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 
}); 

编辑:我不知道,你的函数有两个参数......这使得整个事情更复杂一点。你可以将min和max硬编码到你的函数中(因为函数被插件中的代码调用,并且你绝对不会得到任何参数),或者在它周围包含另一个匿名函数,它为我们提供了两个参数。

最终编辑:我猜你正在使用这个插件https://github.com/harvesthq/tick?这个插件可以接受一个函数,这个函数会在每个时间点被调用并且被传递给ticker的当前值。你必须提供新的价值。我相应地更新了代码段。

+0

这可能不会起作用,因为'incremental'几乎肯定不会作为函数被调用,除非这是插件的内置功能。如果不是,那可能是一个值得添加的功能。 – Pete

+1

我找到了他实际使用的插件,结果证明它可以处理函数。我用插件的实际代码编辑了我的答案。 – sra

+0

酷豆!感谢更新! – Pete

2

你可能有更新增量脚本的来源。但如果你幸运的话,你可能会使用一些欺骗手段。

这有可能是增量脚本有没有这样的事情:

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

如果是这样的话,你可以在事后定期调整的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 

这有点破解,但可能有效。可能比更新插件的代码更容易。如果它不起作用,您可能需要直接增强插件。

相关问题