Jump to content
php.lv forumi
  • 0

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


daGrevis

Question

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

Link to comment
Share on other sites

9 answers to this question

Recommended Posts

  • 0
<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
Link to comment
Share on other sites

  • 0

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?

Link to comment
Share on other sites

  • 0

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
Link to comment
Share on other sites

  • 0

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
Link to comment
Share on other sites

  • 0

ir window.onload = function(); nevis document.onload

Un nevis pati setColor funkcija jāliek onloadā, bet tur ir jāpalaiž tā funkcija

function setColor() {
..
}
window.onload = setColor;

 

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

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

 

			document.onload = function() {

Link to comment
Share on other sites

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