Mark- Posted December 16, 2014 Share Posted December 16, 2014 (edited) Hello, I am not new to programming but, I am new to JavaScript. I have searched the web and looked at countless examples and I have not found a solution. The trouble I am having is with a callback not being called. This code works, all good. function RepeatingRequestTask() { xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) alert('done'); } xmlhttp.open("GET", "pageUpdate.xml?page=" + location.pathname, true); xmlhttp.responseType = "arraybuffer"; xmlhttp.send(); setTimeout(RepeatingRequestTask, 2000);} I wanted to move the callback outside the function so I wrote: function UpdateCallBackCompleter() { if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) alert('done');} I removed the existing onreadstateChange code and added: xmlhttp.onreadstateChange = UpdateCallBackCompleter; That did not work so I changed it to: xmlhttp.onreadstateChange = function() {UpdateCallBackCompleter();}; And that did not work so the hunt for a solution began. I have found many examples that either should work so I am stumped. Maybe something is wrong the the callback function declaration but I have not seen anything that suggest it. Any ideas? Must be something simple. The debugger was no help. Cheers, Mark Edited December 16, 2014 by Mark- Link to comment Share on other sites More sharing options...
Ingolme Posted December 16, 2014 Share Posted December 16, 2014 You've misspelled onreadystatechange as "onreadstateChange". Watch the syntax carefully. Javascript is case sensitive, event names are all in lowercase. Link to comment Share on other sites More sharing options...
Mark- Posted December 16, 2014 Author Share Posted December 16, 2014 Ingolme, That is what happens when I program in languages that are case insensitive and move to ones that are case sensitive. >JavaScript is case sensitive, event names are all in lowercase. That is one I will remember. Thanks for your help. Mark Link to comment Share on other sites More sharing options...
davej Posted December 19, 2014 Share Posted December 19, 2014 Adding an onerror handler can help you find syntax errors or function calls that don't have matching functions. https://dev.opera.com/articles/better-error-handling-with-window-onerror/ Link to comment Share on other sites More sharing options...
Mark- Posted December 19, 2014 Author Share Posted December 19, 2014 Hello, davej, I added the onerror code, thanks. Today I ran into this, I had this code and it was working fine. function IntToColorString(aColor) { return '#' + ('000000' + aColor.toString(16)).substr(-6); } Then sometime last night when I was messing about, I somehow hit the wrong key and added a = to the code. return = '#' + ('000000' + aColor.toString(16)).substr(-6); When I next opened the page I got an error pointing in the totally wrong location. It took me many minutes to find the error. As the code grows larger, this kind of error could be costly. Most other environments I use would have flagged that error at compile time. I just tested with the window.onerror handler and it did not find the error. I had looked at JLint earlier but when I tested it it gave me tons of errors about formatting and I am using the auto format in Aptana so, I have not been using it. I just tested it and JLint caught the error when I pasted just that function. Testing the whole page gave tons of spaces errors. Can anyone recommend a solution? By the by, paste in this editor does not work in IE11. I had to uses Firefox. Thanks for all the help, Mark Link to comment Share on other sites More sharing options...
davej Posted December 19, 2014 Share Posted December 19, 2014 Are you using the browser tools? onerror certainly won't detect many errors but in this error the line identification works for me... <!DOCTYPE html><html lang="en"><head><meta charset="utf-8"/><title>title</title><style></style><script>window.onerror = function(m, u, l){alert('Javascript Error: '+m+'nURL: '+u+'nLine Number: '+l);return true;}</script><script>'use strict';window.onload = init;function init() {document.getElementById('btn1').onclick = put;}function put(){var inp = document.getElementById('colorstr');var strc = inp.value;var strr = IntToColorString(strc);alert('aColor('+ strc +') returned:['+ strr +']');var outc = document.getElementById('outcolor');outc.style.backgroundColor = strr;}function IntToColorString(aColor) { //return '#' + ('000000' + aColor.toString(16)).substr(-6);return = '#' + ('000000' + aColor.toString(16)).substr(-6);}</script></head><body><input type="text" id="colorstr" value="333"/><input type="button" id="btn1" value="Enter"/><div id="outcolor" style="height:50px;"></div><div style="height:50px;background-color:#000333"></div><div style="height:50px;background-color:#333"></div></body> </html> Link to comment Share on other sites More sharing options...
Mark- Posted December 19, 2014 Author Share Posted December 19, 2014 Hello, I am not sure what you are asking. Yes, I am using the debugger in the browser. I am using IE and the indentation did not change with the error. An extra space was added between return and = but, that is hard to detect. I did not check the code in another browser. Mark Link to comment Share on other sites More sharing options...
davej Posted December 20, 2014 Share Posted December 20, 2014 I'm just saying that in the constructed example posted above I see the error location being correctly identified as line 35. Link to comment Share on other sites More sharing options...
Ingolme Posted December 20, 2014 Share Posted December 20, 2014 Which version of Internet Explorer were you testing in? Older versions of Internet Explorer had the line numbers for error messages all wrong and there really is no fix. Develop for modern browsers first, then tweak for Internet Explorer. Link to comment Share on other sites More sharing options...
Mark- Posted December 20, 2014 Author Share Posted December 20, 2014 IE 11. The debugger stopped on a line that was not near the error. Link to comment Share on other sites More sharing options...
davej Posted December 20, 2014 Share Posted December 20, 2014 I know that I have a lot more trouble tracking down errors when I use function expressions rather than function declarations, but I don't see incorrect line numbers. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now