我所拥有的:倒计时器,从3秒到0秒。
<div class="sentence"> This is a sentence.</div>
<p>
<div style='font-family: Arial; font-size: 12px; color:gray'>
<br><span class="timer" id="s2"></span>
<script>
function countdown( elementName, minutes, seconds )
{
var element, endTime, hours, mins, msLeft, time;
function twoDigits( n )
{
return (n <= 9 ? "0" + n : n);
}
function updateTimer()
{
msLeft = endTime - (+new Date);
if ( msLeft < 1000 ) {
element.innerHTML = "<div style='font-family: Arial; font-size: 12px; color:#eda1a1'>0:00</div>";
} else {
time = new Date( msLeft );
hours = time.getUTCHours();
mins = time.getUTCMinutes();
element.innerHTML = (hours ? hours + ':' + twoDigits( mins ) : mins) + ':' + twoDigits( time.getUTCSeconds() );
setTimeout( updateTimer, time.getUTCMilliseconds() + 500 );
}
}
element = document.getElementById( elementName );
endTime = (+new Date) + 1000 * (60*minutes + seconds) + 500;
updateTimer();
}
countdown("s2", 0, 3);//2nd value is the minute, 3rd is the seconds
</script>
</div>
我正在尝试做什么:
将倒数计时器中的秒数更改为“句子”div中的单词数。
例如,如果div包含:
•“这是一个句子”→4个字→倒数计时器从4s变为0
•“这是句子”→3个单词→倒数计时器从3秒变为0秒
•“另一句话”→2个单词→倒数计时器从2秒变为0秒
我已经搜索过谷歌并且什么也没找谢谢你的帮助。
你需要将字符串拆分成一个数组,即
const sentence = document.querySelector('.sentence').innerText.split(' ');
并将参数更新为sentence
的长度,即
countdown("s2", 0, sentence.length);//2nd value is the minute, 3rd is
//只是在空格上拆分div内容
var seconds = yourSentenceDiv.innerHTML.split(' ').length; // * 1000 for setTimeout