Skip to content

Instantly share code, notes, and snippets.

@Dayjo
Created December 8, 2013 02:53
Show Gist options
  • Save Dayjo/7852820 to your computer and use it in GitHub Desktop.
Save Dayjo/7852820 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
</body>
</html>
var meter = (function(object){
var previous_values = [],
max: 100,
min: 0,
value: 0;
if ( typeof object != 'undefined' ) {
if ( typeof object.data != 'undefined' ) {
previous_values = object.data;
}
if ( typeof object.max != 'undefined' ) {
max = object.max;
}
}
var _meter = {
min: min,
max: max,
value: value,
previous_values: previous_values,
auto_increase_max: true,
add: function( addition ) {
if ( typeof addition == "undefined" ) {
addition = 1;
}
this.set_value( this.value + parseFloat(addition) );
return this;
},
subtract: function ( subtraction ) {
if ( typeof subtraction == "undefined" ) {
subtraction = 1;
}
this.set_value( this.value - parseFloat(subtraction) );
return this;
},
set_value: function ( value ) {
if (typeof value == 'undefined') {
return false;
}
if ( this.auto_set_max === true && value > this.max ) {
this.set_max ( value );
}
// if it's not greater than the max, set it
if ( value <= this.max ) {
// Lets just save the previous values
this.previous_values.push(value);
// Now actually set the value of this meter
this.value = parseFloat(value);
}
else {
return false;
}
return this;
},
set_max: function( max ) {
this.max = parseFloat( max );
return this;
}
};
return _meter;
});
var visits = new meter({data: [2,6,7,9,14,21,34]});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment