// Function.prototype.bind polyfill if ( !Function.prototype.bind ) { Function.prototype.bind = function( obj ) { if(typeof this !== 'function') // closest thing possible to the ECMAScript 5 internal IsCallable function throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable'); var slice = [].slice, args = slice.call(arguments, 1), self = this, nop = function () {}, bound = function () { return self.apply( this instanceof nop ? this : ( obj || {} ), args.concat( slice.call(arguments) ) ); }; bound.prototype = this.prototype; return bound; }; } var likeCounter = { // latest count value count: 3936038, // target element selector target: null, // refresh interval (0 to disable refresh) refresh: 0, init: function (params) { if ($(params.target).length == 0) throw 'LikeCounter: Target selector doesn\'t match any dom element.'; if ($(params.target).length > 1) throw 'LikeCounter: Target selector matches more than one element.'; this.target = params.target; this.refresh = params.refresh; $(this.target).text('3.936.038'); if (this.refresh > 0) { setTimeout(this.update.bind(this), this.refresh); } }, update: function () { $.ajax({ url: '/likes/count', type: 'POST', timeout: '30000', context: this, dataType: 'json', success: function (result) { if (result.value > this.context.count) { this.context.count = result.value; $(this.context.target).text(result.valueDisplay); } setTimeout(this.context.update.bind(this.context), this.context.refresh); }, error: function () { setTimeout(this.context.update.bind(this.context), this.context.refresh); } }); } };