Skip to content

Instantly share code, notes, and snippets.

@gabriel-dehan
Last active August 29, 2015 13:59
Show Gist options
  • Select an option

  • Save gabriel-dehan/10688412 to your computer and use it in GitHub Desktop.

Select an option

Save gabriel-dehan/10688412 to your computer and use it in GitHub Desktop.
iPadDigit = (function(selector, api, delay) {
this.selector = $(selector);
this.inputs = [];
this.timer = null;
this.delay = delay;
this.api = api;
});
iPadDigit.prototype.handleInputs = function() {
var self = this;
this.selector.on('click', function() {
var value = $(this).text();
self.inputs.push(value);
self.createTimer();
if (self.inputs.length == 6) {
self.sendData();
self.resetInput();
}
});
};
iPadDigit.prototype.resetInput = function() {
console.log('reset');
this.inputs = [];
clearInterval(this.timer);
};
iPadDigit.prototype.createTimer = function(resetTimer) {
var self = this;
if (this.timer != null) {
clearInterval(this.timer);
}
this.timer = setInterval(function() {
self.resetInput();
}, this.delay);
};
iPadDigit.prototype.sendData = function() {
var url = this.api + '?' + 'digits=' + this.inputs.join();
console.log(url);
$.get(url, function(response) {
console.log(response);
});
};
;(function($){
$.extend($.fn, {
digicode: function(opts){
api = '/' || opts.api;
delay = 10000 || opts.resetDelay * 1000;
digi = new iPadDigit(this, api, delay);
digi.handleInputs();
}
})
})($)
$("ul li").digicode({
api: 'http://www.google.com',
resetDelay: 10
});
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
</ul>
@gabriel-dehan

Copy link
Copy Markdown
Author

@jhchabran

Copy link
Copy Markdown

Nickel thanks !

Btw, pour ce genre de truc, hésite pas à pusher direct dans le repo, si t'es pas sûr, go branche :)

A titre indicatif, j'adore les noms de classes, fonctions et méthodes, mon paragraphe ci-dessous n'est qu'une discussion à ce titre, ce n'est en aucun cas une forme de reproche ou autre cassage de couille

L'une des mes multiples personnalités intitulée Semantic Nazi me fait remarquer que IpadDigit n'est pas forcément le bon nom de classe :)

  • Digit veut dire "chiffre", donc IpadDigit = Chiffre d'ipad

Digicode ou Keypad me semble être les candidats approprié, avec une nette préférence pour Digicode, plus qu'il sous-entend la notion d'accès verrouillé.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment