papa_php Posted December 18, 2009 Share Posted December 18, 2009 hii want to create one js code that do this :1-one counter is counting 20 to 0.2-if counter 0 then choose random string between "click one" , "click two" , "click three" and show it.3-in other table we have three button that name`s are "one" , "two" ,"three".4-for continue page user must click true button,if user click false button alert "wrong".i`m writing one code but don`t work !!!help me,plz.and this is my code : <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head> <title></title><script language="JavaScript" type="text/javascript"> var counterName='countDown'; function start(){ displayCountdown(2-1); } loaded(start); var pageLoaded=0; window.onload=function(){ pageLoaded=1; } function loaded(functionName) { if( document.getElementById && document.getElementById(counterName) != null){ functionName(); } else { if(!pageLoaded){ setTimeout('loaded('+functionName+')',100); } } } function displayCountdown(counter) { if(counter <= 0){ var mess=["one" , "two" , "three"]; var r=Math.floor(Math.random()*mess.length); document.getElementById('test').innerHTML='click '+mess[r]; } else{ document.getElementById(counterName).innerHTML=counter; setTimeout('displayCountdown('+(counter-1)+');',1000); } }</script></head><body><span id="countDown" value="" ></span><span id="test" value="" ></span></body></html> thx Link to comment Share on other sites More sharing options...
jeffman Posted December 18, 2009 Share Posted December 18, 2009 For starters, you probably want to change 2-1 to 21, since that is what starts your countdown. I also made the counter variable global to avoid possible memory leaks. Not a big deal if you're running your function 20 times, but if you ever wanted to run it a million times, there might be a problem.Your onload setup is also needlessly complicated and might not even work in some browsers, since you were calling the start() function before it was actually loaded (which defeats the whole point of using window.onload). So I've simplified that. Actually, I've simplified everything. I didn't add any buttons because I wasn't sure if that was part of your problem. <script type="text/javascript"> var counterName='countDown'; var counter = 21; function displayCountdown() { --counter; if (counter <= 0){ var mess=["one" , "two" , "three"]; var r=Math.floor(Math.random() * mess.length); document.getElementById('test').innerHTML='click ' + mess[r]; } else { document.getElementById(counterName).innerHTML = counter; setTimeout(displayCountdown, 1000); } } window.onload = displayCountdown;</script> Link to comment Share on other sites More sharing options...
papa_php Posted December 18, 2009 Author Share Posted December 18, 2009 thx.but one of my problem was button function !!!! Link to comment Share on other sites More sharing options...
justsomeguy Posted December 18, 2009 Share Posted December 18, 2009 Yeah Dad, why didn't you write his button function for him? Geez.Do you have a button on the page? You can create a button element and give it whatever text you want, and assign a click handler to it, e.g.: var button = document.createElement('button');button.innerHTML = 'Button One';button.onclick = function(){ alert('You clicked button one');}document.getElementById('test').appendChild(button); Link to comment Share on other sites More sharing options...
papa_php Posted December 18, 2009 Author Share Posted December 18, 2009 thx.this code part of the game.and if user`s answer true, increasing user`s score and if false decreasing user`s score, so this code must be secure.how to secure this code ? Link to comment Share on other sites More sharing options...
justsomeguy Posted December 18, 2009 Share Posted December 18, 2009 This is Javascript, you can't secure Javascript. Anyone can set the value of any variable to anything they want. If you want it to be secure all processing needs to be done through PHP, and you need to be able to validate everything to make sure they the user is allowed to do a certain thing. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.