Created
August 24, 2018 21:20
-
-
Save olegpolyakov/71c59b3482904bb3d607299859e6b72d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var $choices = $('.choice'); | |
var $total = $('.total .number'); | |
$choices.on('click', function (event) { | |
var price = $(this).data('price'); | |
$total.text(price); | |
changeNumber('.total .number'); | |
}); | |
function getRandomNumber(max) { | |
return Math.floor(Math.random() * 10); | |
} | |
function getRandomDigits() { | |
var randoms = []; | |
var int; | |
while (randoms.length < 10) { | |
int = getRandomNumber(10); | |
if ((randoms.indexOf(int) === -1)) { | |
randoms.push(int); | |
} | |
} | |
return randoms; | |
} | |
function changeNumber() { | |
var digits = $total.text().split(''); | |
var $digits; | |
$total.text(''); | |
$digits = digits.map(function (digit, index) { | |
var $span = $('<span>').addClass('digit').text(digit); | |
$total.append($span); | |
return $span; | |
}); | |
Array.prototype.forEach.call(digitSpans, function (span) { | |
var value = span.innerText; | |
var randoms = getRandomDigits(); | |
var i = 0; | |
var interval = setInterval(function () { | |
span.innerText = randoms[i]; | |
i += 1; | |
if (i === randoms.length) { | |
clearInterval(interval); | |
span.innerText = value; | |
} | |
}, 25); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment