Answer the question
In order to leave comments, you need to log in
JavaScript code optimization. How is it better?
The bottom line is this: PHP generates a set of numbers on the server, for example:
$numbers = array('+1','+6','-4','+3','+4','-1','+7','+2','-5','+1','-2','+5','-4','+2');
foreach($numbers as $index => $number){?>
interval = setTimeout(function() {
$("#number").html("<?=$number?>");//Показ слагаемого
sound.play('peek')//Озвучивание
}, <?=(3800+($index*1000))?>);
<?}
Answer the question
In order to leave comments, you need to log in
Let's display all the numbers so that they can be picked up in js
<span id="numbers" style="display: none;"><?=implode(',', $numbers)?></span>
<pre id="display">start</pre>
(function(){
var numbers = document.querySelector('#numbers').innerHTML.split(','), // распарсим числа
delay = 1000, // задержка между цифрами, msec
len = numbers.length,
counter = 0,
display = document.querySelector('#display'),
timer = setInterval(function(){ // запустим таймер
if (counter < len) { // если еще не добрались до конца массива
display.innerHTML = numbers[counter++] // выводим число
} else {
clearInterval(timer) // или выключаем таймер
display.innerHTML = 'end!'
}
}, delay)
})()
https://developer.mozilla.org/en-US/docs/Web/Guide...
https://developer.mozilla.org/en-US/docs/Web/Event...
https://github. com/kriskowal/q (for convenience only)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question