2012-09-18 44 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 

這有點破解,但可能有效。可能比更新插件的代碼更容易。如果它不起作用,您可能需要直接增強插件。

相關問題