function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JohnTaylorUKJohnTaylorUK 

Pass value of a Javascript Timer into a SFDC field on save ????

Hi All,

 

I have built a little javascript timer that wii eventually start when a user visits a html page (soon to be a sites page).

 

I am trying to work out if it would be possible (and how..) to pass the variable from that timer to a field on an object.

 

I've included the code below if that helps, and im sure im going to need an add an ID= here and there but am a little bit stuck when it comes to writting the controller...

 

Any help to just work out if this is possible would be great..

 

Many Thanks to you all...:smileywink:

 

 

<HTML>


<HEAD>

<script language="JavaScript" type="text/javascript">
var sec = 0;
var min = 0;
var hour = 0;
function stopwatch(text) {
   sec++;
  if (sec == 60) {
   sec = 0;
   min = min + 1; }
  else {
   min = min; }
  if (min == 60) {
   min = 0; 
   hour += 1; }

if (sec<=9) { sec = "0" + sec; }
   document.clock.stwa.value = ((hour<=9) ? "0"+hour : hour) + " : " + ((min<=9) ? "0" + min : min) + " : " + sec;

  if (text == "Start") { document.clock.theButton.value = "Stop "; }
  if (text == "Stop ") { document.clock.theButton.value = "Start"; }

  if (document.clock.theButton.value == "Start") {
   window.clearTimeout(SD);
   return true; }
SD=window.setTimeout("stopwatch();", 1000);
}

function resetIt() {
  sec = -1;
  min = 0;
  hour = 0;
  if (document.clock.theButton.value == "Stop ") {
  document.clock.theButton.value = "Start"; }
  window.clearTimeout(SD);
 }
 
</script>

</HEAD>

<BODY>


<table bgcolor="#c0c0c0" align="center" border="2" width="140" cellspacing="0">

  <tr>
     <td align="center">
       <form name="clock">
       <input type="text" size="12" name="stwa" value="00 : 00 : 00" style="text-align:center" /><br />
       <input type="button" name="theButton" onClick="stopwatch(this.value);" value="Start" />
       <input type="button" value="Reset" onClick="resetIt();reset();" />
       </form>
     </td>
  </tr>     
</table>
</HTML>

 

 

JohnTaylorUKJohnTaylorUK

Hi All,

 

After some work I did manage to sort this out, and without a controller.

 

Thanks does have to go to Wes Nolte and Jeff Douglas for their support in this.

 

But below so can see the entire code to build a survey / Logger type tool , with a timer and a button to start it , that will save the time value and all the other fields into the object.

 

You could easily chnage this to start the timer when a user hits the page by calling the script using OnLoad.

 

I hope this helps someone out there in the fture.

 

p.s (Please forgive the really nasty spacing of the form elements to get them to sit nicely on the page ..)

 

 

<apex:page standardController="RCA_Logger__c" showHeader="false" extensions="Logger_Extend">

<Head>
<script language="JavaScript" type="text/javascript">


var sec = 0;
var min = 0;
var hour = 0;
function stopwatch(text) {
   sec++;
  if (sec == 60) {
   sec = 0;
   min = min + 1; }
  else {
   min = min; }
  if (min == 60) {
   min = 0; 
   hour += 1; }

if (sec<=9) { sec = "0" + sec; }
   document.clock.stwa.value = ((hour<=9) ? "0"+hour : hour) + " : " + ((min<=9) ? "0" + min : min) + " : " + sec;

  if (text == "Start") { document.clock.theButton.value = "Stop "; }
  if (text == "Stop ") { document.clock.theButton.value = "Start"; }

  if (document.clock.theButton.value == "Start") {
   window.clearTimeout(SD);
   return true; }
SD=window.setTimeout("stopwatch();", 1000);
}

function resetIt() {
  sec = -1;
  min = 0;
  hour = 0;
  if (document.clock.theButton.value == "Stop ") {
  document.clock.theButton.value = "Start"; }
  window.clearTimeout(SD);
 }

</script>
</Head>


<body bgcolor="Red">

<BR/>








       <form name="clock">
       <input type="button" name="theButton" onClick="stopwatch(this.value);" value="Start" /> 
       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
       <input type="text" size="12" name="stwa" value="00 : 00 : 00" style="text-align:center" />
       

       
       
       </form>
    
<BR/>
    
    <apex:form >

    <apex:inputHidden value="{!RCA_Logger__c.Time__c}" id="theField" />
    <script> var theField = document.getElementById("{!$Component.theField}"); </script>

     &nbsp;<B> Type Of Contact &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :  </B> <apex:inputField id="Type" value="{!RCA_Logger__c.Type__c}" style="width:250px" /> <BR/>
     <BR/>
   
    
     &nbsp;<B> Business Unit &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :  </B> <apex:inputField id="Unit" value="{!RCA_Logger__c.Business_unit__c}" style=" width:250px " /> <BR/>
     <BR/>

    
               
     &nbsp;<B> Inbound / Outbound &nbsp; : </B> <apex:inputField value="{!RCA_Logger__c.Level_1__c}" style=" width:250px " /> <BR/>
     <BR/>
     
     &nbsp;<B> Level 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;</B> 
     <apex:inputField value="{!RCA_Logger__c.Level_2__c}" style=" width:250px "  id="Level1" /> <BR/>
     <BR/>
     
     &nbsp;<B> Level 2 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;</B> 
     <apex:inputField value="{!RCA_Logger__c.Level_3__c}" style=" width:250px "  id="Level2"/> <BR/>
     <BR/>
     
     &nbsp;<B> Level 3 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;</B> 
     <apex:inputField value="{!RCA_Logger__c.Level_4__c}" style=" width:250px " id="Level3"/> <BR/>
     
     
     
     
    

    <BR/>
    <apex:commandButton onclick="theField.value=document.clock.stwa.value; " action="{!SaveLead}" value="Complete & Save"/>
    </apex:form>    
   
  </body>
</apex:page>