www.webdeveloper.com
Results 1 to 7 of 7

Thread: Need urgent help please - Call function twice!

  1. #1
    Join Date
    Aug 2012
    Posts
    6

    Question Need urgent help please - Call function twice!

    Hey guys,

    Please have a look at the below page and its code.
    http://goo.gl/x40L2

    Now, if you look at the source of that page and Ctrl + F for function EpochToHuman1(), you'll find two instances of this function and for a few other functions too with exactly same code (two instances of the whole code actually starting from this function declaration).

    On the frontend, there are two rows with some data, each row and its content is generated by one instance of that code. The complete second instance of the code is dynamically generated that outputs the second row. So basically this complete code is in a template that will always loop.

    You'll notice that the correct values (Unix timestamp values) from the database are there in the code in their respective order within the function and is stored in the variables.

    The problem I'm having is that the second row's data is being displayed in the first row and the first row data is never displayed (although its there as I see in the code).

    I understand the code is not very optimized but that is the only way I could do this. So any help is really appreciated in order to resolve this problem, a dirty trick that does the job will do as well.

    Also please note that I'm unable to alter the second instance of the complete code as it is generated dynamically at the server side, so changing function/variable names (if required) will not be possible unless we change it at the client side I guess.

    Thanks

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,350

    Lightbulb

    Have you tried eliminating the second copy of the duplicated script.
    I did here and see no difference in the display...
    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">
    <!-- BC_OBNW -->
    <head>
    <title>Timesheet Report</title>
    <link href="/StyleSheets/ModuleStyleSheets.css" type="text/css" rel="StyleSheet" />
    <script type="text/javascript">var jslang='EN';</script>
    <link rel="stylesheet" href="/CatalystStyles/Box.css" type="text/css" media="screen" />
    <script type="text/javascript" src="/CatalystScripts/Java_Box.js"></script>
    
    </head>
    <body>
    <p><strong><span style="font-size: 22px;">Team Report</span></strong></p>
    <table border="2" width="100%" style="table-layout: fixed;">
        <tbody>
            <tr>
                <th>Name</th>
                <th>Login Time</th>
                <th>On Break Since</th>
                <th>Total Break Time</th>
                <th>Log Out Time</th>
                <th>Total Login Time</th>
            </tr>
        </tbody>
    </table>
    <table border="2" width="100%" style="table-layout: fixed;">
        <tbody>
            <tr>
                <td style="text-align: center;">Parik**** 28/8/2012 1:29<br />
                </td>
                <td>
                <div id="result1"></div>
                </td>
                <td>
                <div style="background-color: red;" id="result2"></div>
                </td>
                <td>
                <div id="result3"></div>
                </td>
                <td>
                <div id="result4"></div>
                </td>
                <td>
                <div id="result5"></div>
                </td>
            </tr>
        </tbody>
    </table>
    <script type="text/javascript">
    function addLoadEvent(func) {
      var oldonload = window.onload;
      if (typeof window.onload != 'function') {
        window.onload = func;
      } else {
        window.onload = function() {
          if (oldonload) {
            oldonload();
          }
          func();
        }
      }
    }
    addLoadEvent(EpochToHuman1);
    if (breaktime != ""){
    addLoadEvent(EpochToHuman2);
    }
    if (totalbreaktime != ""){
    addLoadEvent(EpochToHuman3);
    }
    if (logouttime != ""){
    addLoadEvent(EpochToHuman4);
    }
    if (totallogintime != ""){
    addLoadEvent(EpochToHuman5);
    }
    addLoadEvent(function(){})
    </script>
    <script type="text/javascript">
    function EpochToHuman1(){
    	var inputtext = 1346091991000;
    	var epoch=inputtext;
    	var outputtext = "";
    	var extraInfo = 0;
    	if(inputtext > 1000000000000){
    		epoch=Math.round(inputtext/1000);
    	}else{
    		if(inputtext > 10000000000)extraInfo=1;
    		inputtext=(inputtext*1000);
    	}
    	var datum = new Date(inputtext);
    	var localeString = datum.toLocaleString();
    	var localeStringEnd = localeString.search(/GMT/i);
    	if(localeStringEnd>0){ localeString=localeString.substring(0,localeStringEnd); }
    	outputtext += localeString;
    	document.getElementById('result1').innerHTML = outputtext;
    }
    var breaktime = "1346091992501";
    if (breaktime != ""){
    breaktime = parseInt(breaktime);
    }
    function EpochToHuman2(){
    	var inputtext = breaktime;
    	var epoch=inputtext;
    	var outputtext = "";
    	var extraInfo = 0;
    	if(inputtext > 1000000000000){
    		epoch=Math.round(inputtext/1000);
    	}else{
    		if(inputtext > 10000000000)extraInfo=1;
    		inputtext=(inputtext*1000);
    	}
    	var datum = new Date(inputtext);
    	var localeString = datum.toLocaleString();
    	var localeStringEnd = localeString.search(/GMT/i);
    	if(localeStringEnd>0){ localeString=localeString.substring(0,localeStringEnd); }
    	outputtext += localeString;
        document.getElementById('result2').innerHTML = outputtext;
    }
    var totalbreaktime = "3253255";
    if (totalbreaktime != ""){
    totalbreaktime = parseInt(totalbreaktime);
    }
    function EpochToHuman3(){
    var inputtext = totalbreaktime;
    milliSecs = inputtext;
    msSecs = (1000)
    msMins = (msSecs * 60)
    msHours = (msMins * 60)
    numHours = Math.floor(milliSecs/msHours)
    numMins = Math.floor((milliSecs - (numHours * msHours)) / msMins)
    numSecs = Math.floor((milliSecs - (numHours * msHours) - (numMins * msMins))/ msSecs)
    if (numSecs < 10){
      numSecs = "0" + numSecs
    }
    if (numMins < 10){
      numMins = "0" + numMins
    }
    if (numHours < 10){
      numHours = "0" + numHours
    }
    outputtext = numHours + ":" + numMins + ":" + numSecs;
    document.getElementById('result3').innerHTML = outputtext;
    }
    var logouttime = "1346091399999";
    if (logouttime != ""){
    logouttime = parseInt(logouttime);
    }
    function EpochToHuman4(){
    	var inputtext = logouttime;
    	var epoch=inputtext;
    	var outputtext = "";
    	var extraInfo = 0;
    	if(inputtext > 1000000000000){
    		epoch=Math.round(inputtext/1000);
    	}else{
    		if(inputtext > 10000000000)extraInfo=1;
    		inputtext=(inputtext*1000);
    	}
    	var datum = new Date(inputtext);
    	var localeString = datum.toLocaleString();
    	var localeStringEnd = localeString.search(/GMT/i);
    	if(localeStringEnd>0){ localeString=localeString.substring(0,localeStringEnd); }
    	outputtext += localeString;
        document.getElementById('result4').innerHTML = outputtext;
    }
    var totallogintime = "5235632";
    if (totallogintime != ""){
    totallogintime = parseInt(totallogintime);
    }
    function EpochToHuman5(){
    var inputtext = totallogintime;
    milliSecs = inputtext;
    msSecs = (1000)
    msMins = (msSecs * 60)
    msHours = (msMins * 60)
    numHours = Math.floor(milliSecs/msHours)
    numMins = Math.floor((milliSecs - (numHours * msHours)) / msMins)
    numSecs = Math.floor((milliSecs - (numHours * msHours) - (numMins * msMins))/ msSecs)
    if (numSecs < 10){
      numSecs = "0" + numSecs
    }
    if (numMins < 10){
      numMins = "0" + numMins
    }
    if (numHours < 10){
      numHours = "0" + numHours
    }
    outputtext = numHours + ":" + numMins + ":" + numSecs;
    document.getElementById('result5').innerHTML = outputtext;
    }
    </script>
    <table border="2" width="100%" style="table-layout: fixed;">
        <tbody>
            <tr>
                <td style="text-align: center;">Vinayak 27/8/2012 23:56<br />
                </td>
                <td>
                <div id="result1"></div>
                </td>
                <td>
                <div style="background-color: red;" id="result2"></div>
                </td>
                <td>
                <div id="result3"></div>
                </td>
                <td>
                <div id="result4"></div>
                </td>
                <td>
                <div id="result5"></div>
                </td>
            </tr>
        </tbody>
    </table>
    <script type="text/javascript">
    function addLoadEvent(func) {
      var oldonload = window.onload;
      if (typeof window.onload != 'function') {
        window.onload = func;
      } else {
        window.onload = function() {
          if (oldonload) {
            oldonload();
          }
          func();
        }
      }
    }
    addLoadEvent(EpochToHuman1);
    if (breaktime != ""){
    addLoadEvent(EpochToHuman2);
    }
    if (totalbreaktime != ""){
    addLoadEvent(EpochToHuman3);
    }
    if (logouttime != ""){
    addLoadEvent(EpochToHuman4);
    }
    if (totallogintime != ""){
    addLoadEvent(EpochToHuman5);
    }
    addLoadEvent(function(){})
    </script>
    
    </body>
    </html>
    I believe in your version, the script executed the duplicated functions and ignores the 1st copy.

  3. #3
    Join Date
    Aug 2012
    Posts
    6
    Hi JMRKER,

    Thanks for your reply! Like I had said earlier, there's not really a way I can eliminate the second copy of the duplicated script as its generated out of an uncontrolled loop (thats how the system is). And doing so would not solve the problem either. The loops must happen.

    I have tried fiddling around with a copy of that page locally, and it seems that the similar function names is not really a problem but its the display variable name (result1, result2, etc..) being similar in both the instances that is blocking the values to display for an older entry.

    If I change them to being unique somehow, it starts displaying the value. For example, if you look at the function EpochToHuman1(), it is using the below code to capture the value from the element ID "result1".

    HTML Code:
    document.getElementById('result1').innerHTML = outputtext;
    And in my HTML, I have:

    HTML Code:
    <div id="result1"></div>
    The above does the job fine, but for the second instance of the code, the result1 creates the block as it already has a value and overrides with the last one.

    Now, I think dynamically changing all instances of resultx to being unique and having them inserted in the above two places throughout the loops should most likely resolve the problem. What do you think?

    So the original code currently has result1, result2 etc.. in all functions for getElementById and they are being displayed as is. Need to figure out a way to dynamically replace the resultx thing with automatically incremental resultx's in the above two lines of code.

  4. #4
    Join Date
    Aug 2012
    Posts
    6
    Or is there a way to simply clear out everything (meaning the executing functions) EXCEPT leaving the values on display (i.e. save them in memory), and execute the next duplicate instance of code again while going forward. Can we put a "Stop executing but keep displaying" kinda thing right after the first instance ends?

  5. #5
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,350

    Question

    Quote Originally Posted by webwizzy View Post
    ...
    Also please note that I'm unable to alter the second instance of the complete code as it is generated dynamically at the server side, so changing function/variable names (if required) will not be possible unless we change it at the client side I guess.

    Thanks
    So who is generating the duplicated code on the server side?
    Why do they (or you) generate it twice? For what purpose?

  6. #6
    Join Date
    Aug 2012
    Posts
    6
    Quote Originally Posted by JMRKER View Post
    So who is generating the duplicated code on the server side?
    Why do they (or you) generate it twice? For what purpose?
    The page is built on an Adobe platform, and the code resides in a LIST template, that lists all the items in a web app. So for example, I have 100 entries in my database with email addresses, all 100 would list on a page. Each of the item would utilize that LIST template.

    In my case, I have all that code in the LIST template, so it is being outputted as many times as I have entries in that web app. On my page, each data row you see (after the header row) is the result of the code I have in the LIST template. I have 2 items currently for the web app, hence 2 rows.

    As the platform does not allow server side coding, much of the stuff needs to be done client side. I have Unix timestamps in the web app fields currently, that I am modifying to be readable on frontend via JavaScript, hence so much code.

  7. #7
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,350
    My understand is this...

    1. You have a server that is generating code from a list you submit.
    2. But you cannot change the server-side code nor the fact that it duplicates code depending upon your list.
    3. Since javascript is a client-side language, I do not see anyway around this.
    It cannot change the server-side display information being sent.

    Sorry, I'm not sure I can be of much help!
    Maybe some other forum members have some alternative suggestions.

    Good Luck.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles