4 Replies - 1187 Views - Last Post: 11 April 2012 - 10:35 PM

#1 sonalmac   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 24-June 11

why this function is not getiing "call"?

Posted 11 April 2012 - 02:32 PM

I'm trying to record how much time user is taking to read a piece of text. I'm using ajax to fetch a text file and show it to html page. IT works fine, but the corresponding time watching functions are not showing any effect. To make the question clear, I'm copying my script here. Following is JS script. ajax object is defined in other file, and it is working fine. This script is included by a html page with two button elements and a div to display text.
/* 
 * This script uses ajax object defined in ajax.js file, to load content from
 * ajaxtest.html.
 */
//global variables to store start time, end time and reading time.
stime = 0;
etime = 0;
rtime = 0;

function watchTime()
     {
	    
	   //get current time into minutes:
	   var today = new Date();
	   var h = today.getHours(); //get hour part of the day.
	   var m = today.getMinutes(); //get minute part of the day.
	   var mtemp = m * 60;
	   var s = today.getSeconds(); //get second part of the day.
	   	   
	   return mtemp + s;
	   }
  

function startReading() {
    'use strict';
    stime = watchTime();
    console.log(stime);
    
}

function endReading() {
    'use strict';
          
    etime = watchTime();
    console.log(etime);
    rtime = etime - stime;
    console.log(rtime);
}


window.onload = function() {
    'use strict';
     document.getElementById('stbtn').onclick = function() {
         startReading();     //This function is not getting "call".
        ajax.open('GET', 'resources/test.txt', true);
        ajax.send(null);
        
    }
    
    var ajax = getXMLHttpRequestObject();
    
    //create function for onreadystatechange
    ajax.onreadystatechange = function() {
        if(ajax.readyState == 4) {
            if((ajax.status >= 200 && ajax.status < 300) || (ajax.status == 304))
            {
                document.getElementById('output').innerHTML = ajax.responseText;
            } else {
                document.getElementById('output').innerHTML = 'Error' + ajax.statusText;
            }
        }  //end of readyState IF.
    }  //End of main function. 
    
     document.getElementById('endbtn').onclick = endReading;  //This line does not show any effect.
}




Is This A Good Question/Topic? 0
  • +

Replies To: why this function is not getiing "call"?

#2 e_i_pi   User is offline

  • = -1
  • member icon

Reputation: 879
  • View blog
  • Posts: 1,893
  • Joined: 30-January 09

Re: why this function is not getiing "call"?

Posted 11 April 2012 - 03:14 PM

This is where using something like Firebug comes in very handy. I imagine that the problems are that you are not closing off statement with semicolons, and you aren't calling the endReading function properly. I don't have my dev kit in front of me, but try this:
window.onload = function() {
    'use strict';
     document.getElementById('stbtn').onclick = function() {
         startReading();     //This function is not getting "call".
        ajax.open('GET', 'resources/test.txt', true);
        ajax.send(null);
        
    };
    
    var ajax = getXMLHttpRequestObject();
    
    //create function for onreadystatechange
    ajax.onreadystatechange = function() {
        if(ajax.readyState == 4) {
            if((ajax.status >= 200 && ajax.status < 300) || (ajax.status == 304))
            {
                document.getElementById('output').innerHTML = ajax.responseText;
            } else {
                document.getElementById('output').innerHTML = 'Error' + ajax.statusText;
            }
        }  //end of readyState IF.
    };  //End of main function. 
    
     document.getElementById('endbtn').onclick = endReading();  //This line does not show any effect.
}


Was This Post Helpful? 0
  • +
  • -

#3 sonalmac   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 24-June 11

Re: why this function is not getiing "call"?

Posted 11 April 2012 - 03:38 PM

View Poste_i_pi, on 11 April 2012 - 03:14 PM, said:

you aren't calling the endReading function properly.


At that, point I'm assigning the function to the button's onclick property. It gets called only when the button gets clicked. So, problem may be somewhere else.
Was This Post Helpful? 0
  • +
  • -

#4 e_i_pi   User is offline

  • = -1
  • member icon

Reputation: 879
  • View blog
  • Posts: 1,893
  • Joined: 30-January 09

Re: why this function is not getiing "call"?

Posted 11 April 2012 - 06:33 PM

Ah yes of course, sorry about that. Have you tried putting semicolons on the end of lines 48 and 62? I believe anonymous function assignments still need semicolons to close out the line.
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4217
  • View blog
  • Posts: 13,328
  • Joined: 08-June 10

Re: why this function is not getiing "call"?

Posted 11 April 2012 - 10:35 PM

line #24 is wrong. you pass a function to the onevent properties not a function return value.
document.getElementById('endbtn').onclick = endReading;

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1