Jump to content
php.lv forumi
  • 0

Fn, kas izsauc fn, kas izsauc fn, kas izsauc fn, kas izsauc fn, kas iz


Question

Posted

Pirmais skripts '11! Ideja ir, ka kastītes krāsas mainās ik pēc sekundes sākot no sarkana, tad pārejot uz zaļu, tad zilu. Un atkal uz apli. Tagad skripts padara kastīti zilu. Un nekas nemainās.

 

http://pastie.org/1420403

9 answers to this question

Recommended Posts

  • 0
Posted (edited)
<script type="text/javascript">
var color_list = ['red','green','blue'];
var color = 0;

function setColor() {

	document.getElementById( 'box' ).style.background = color_list[color];
	color += 1;
	if(color>=color_list.length){color = 0;}
	setTimeout('setColor()', 1000 );

/* OR
	setInterval(function(){
		document.getElementById( 'box' ).style.background = color_list[color];
		color += 1;
		if(color>=color_list.length){color = 0;}
	},1000);
*/


}
</script>

Edited by sheps
  • 0
Posted

Paldies abiem. =)

 

			var colors = [ '#ff0000','#ffa500','#ffff00', '#008000', '#0000ff', '#4b0082', '#ee82ee' ];
		var color_id = 0;

		function setColor() {

			document.getElementById( 'box' ).style.background = colors[ color_id ];

			color_id++;

			if( color_id >= colors.length ) {

				color_id = 0;

			}

			setTimeout( 'setColor()', 10 );

		}

 

Kaut kur dzirdēju, ka nav labi izmantot HTML atribūtus 'onload', 'onclick' utml.. Kāda ir alternatīva?

  • 0
Posted (edited)

Nav labi, ja nemāk un/vai negrib čakarēties. Bet, visticamāk, alternatīva bija domāta apmēram šāda:

 

$(document).ready(function(){
	$('a').click(function(){
			alert('Oh yea');
			return false;
	});
});

(izmantots jQuery - idejai būtu jābūt skaidrai)

Edited by marrtins
  • 0
Posted

Jā, jQuery Es tā visu laiku daru. Nē, man nav skaidrs kā to dabūt tīrā JS... =(

  • 0
Posted (edited)

http://www.webreference.com/programming/javascript/onloads/

http://www.onlinetools.org/articles/unobtrusivejavascript/chapter4.html

http://stackoverflow.com/questions/191157/window-onload-vs-body-onload

 

Tikai nezinu, kādēļ nestrādā... =(

 

			document.onload = function() {

			var colors = [ '#ff0000','#ffa500','#ffff00', '#008000', '#0000ff', '#4b0082', '#ee82ee' ];
			var color_id = 0;

			function setColor() {

				document.getElementById( 'box' ).style.background = colors[ color_id ];

				color_id++;

				if( color_id >= colors.length ) {

					color_id = 0;

				}

				setTimeout( 'setColor()', 100 );

			}

		}

Edited by daGrevis

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...