5 Replies - 497 Views - Last Post: 25 July 2014 - 06:09 PM

#1 mored  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 31-August 12

need button to clear time -- update

Posted 25 July 2014 - 11:23 AM


<html>

<head>

<title>Javascript Testbed</title>

<script type="text/javascript"> 
function display_c(){
var refresh=1000; // Refresh rate in milli seconds
mytime=setTimeout('display_ct()',refresh)
}

function display_ct() {
var strcount
var x = new Date()
document.getElementById('ct').innerHTML = x;
tt=display_c();
 }
 
 
 
function display_ct() {
var strcount
var x = new Date()
var x1=x.toUTCString();// changing the display to UTC string
document.getElementById('ct').innerHTML = x1;
tt=display_c();
 } 
 
 
 
 function hideElement(id_attribute_value) {
	if (document.getElementById &&
	  document.getElementById(id_attribute_value) &&
	  document.getElementById(id_attribute_value).style
	  ) {
	  document.getElementById(id_attribute_value).style.visibility = "hidden";
	  };
	}    
 
 
 
</script>

</head>

<body onload=display_ct();>

<h1>
Javascript will find out the date and the browser.
</h1>

<button type="button" onclick="hideElement('d1');">hide div</button>
<span id='ct' ></span>


</body>

</html>



The button doesn't clear the time --- any suggestions?

Is This A Good Question/Topic? 0
  • +

Replies To: need button to clear time -- update

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1184
  • View blog
  • Posts: 7,254
  • Joined: 07-September 06

Re: need button to clear time -- update

Posted 25 July 2014 - 11:27 AM

Take a look at the ID of the element the time is being stored in, and the ID of the element you are telling the button to hide.
Was This Post Helpful? 0
  • +
  • -

#3 mored  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 31-August 12

Re: need button to clear time -- update

Posted 25 July 2014 - 11:30 AM

can you send me sample code, if possible?
Was This Post Helpful? 0
  • +
  • -

#4 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1184
  • View blog
  • Posts: 7,254
  • Joined: 07-September 06

Re: need button to clear time -- update

Posted 25 July 2014 - 12:02 PM

So... I take it that the above code is copied and pasted from other sources...

In short, no I won't do your work for you (nor will the other members of the forum) -- you get to learn, that's what DIC is all about.

Super big hint -- look at lines 53 and 54.
Was This Post Helpful? 0
  • +
  • -

#5 Blindman67  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 132
  • View blog
  • Posts: 585
  • Joined: 15-March 14

Re: need button to clear time -- update

Posted 25 July 2014 - 02:41 PM

You are passing the wrong ID to the function you call with the button click.

Try changing onclick="hideElement('d1');" to onclick="hideElement('ct');"

Also it looks like you are wanting to get a value from the function display_c when you call it at line 27
tt=display_c();

But the function display_c is not returning the value you want. To return a value you need to use the return token. Change line 10 from
mytime=setTimeout('display_ct()',refresh)

to
return setTimeout(display_ct,refresh); // this returns the handle setTimeout returns.

Also you do not need to put the quotes around the function you want timeout to call. In fact its considered bad practice and is very slow compared to just the function reference. Just use the function name without the ();

Also you are referencing a few variables without using the var token. It is very important that you always declare varibile with the var keyword as it defines the variables scope (what parts of the page can see the variable). Without the var token the variable is placed in global scope (that is all code on the page can see and use it). This can cause all sorts of problems when your code gets bigger.

var foo; // this is global scope
function bar(){
   var poo;  // this is local function scope and can only be seen in this function
   poopoo = 1; // missing the var this becomes global scope
}



I am a firm believer in teaching by example. I will not do your code directly but I am more than happy to give you all the code examples and corrections needed to get you going. Feel free to ask if you have any other problems.
Was This Post Helpful? 0
  • +
  • -

#6 mored  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 31-August 12

Re: need button to clear time -- update

Posted 25 July 2014 - 06:09 PM

Thank you Blindman67. You're absolutely right --- teaching by example is just as valid as any other teaching methodology. Your code helped, and I am grateful for your help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1