Jump to content
php.lv forumi
  • 0

javascript taimeris


Question

Posted

Sveiki, vai varat iedot, kkādu paraugu, kur ir parādīts kā javascript izpilda timeri. Tas ir tā, ka piemēram no 20 sekundēm atskaita lidz 0 un tad vnk paradās kautkāds teksts?

5 answers to this question

Recommended Posts

  • 0
Posted
<div id="sek">20</div>
<script type="text/javascript">
var taimers = null;

function skaitamSek(sec){
 if (taimers) { clearTimeout(taimers); }
 taimers = setTimeout(function(){
  document.getElementById('sek').innerHTML = --sec;
  if (sec <= 0) { 
   document.getElementById('sek').innerHTML = 'Done';
  }
  else {
    skaitamSek(sec);
  }
 },1000);
}

skaitamSek(20);

</script>

  • 0
Posted

setInterval():

<div id="sek">20</div>
<script type="text/javascript">
var
	s = document.getElementById("sek"),
	interval = window.setInterval(function(){
		if (s.innerHTML <= 0)
		{
			window.clearInterval(interval);
			s.innerHTML = 'Done';
		}
		else
		{
			s.innerHTML = --s.innerHTML;
		}
	},
1000);

</script>

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...