User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 423,681 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,224 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 3327 | Replies: 1
Reply
Join Date: Feb 2007
Posts: 56
Reputation: adaykin is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
adaykin adaykin is offline Offline
Junior Poster in Training

Question Function is undefined

  #1  
Dec 23rd, 2007
Hello, I get an error message saying flow() is not defined when I debug in firebug. I am not sure why this is happening. Here is my code:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <title>Image Flow</title>
  6. <script type="text/javascript">
  7. function imageFlow()
  8. {
  9. var R = 0;
  10. var x1 = .1;
  11. var y1 = .05;
  12. var x2 = .25;
  13. var y2 = .24;
  14. var x3 = 1.6;
  15. var y3 = .24;
  16. var x4 = 300;
  17. var y4 = 200;
  18. var x5 = 300;
  19. var y5 = 200;
  20. DI = document.images;
  21. DIL = DI.length;
  22. function flow()
  23. {
  24. for(var i = 0; i < DIL; i++)
  25. {
  26. DIS = DI[i].style;
  27. DIS.position = 'absolute';
  28. DIS.left = Math.sin(R * x1+ i * x2 + x3) * x4 + x5;
  29. DIS.top = Math.cos(R * y1 + i * y2 + y3) * y4 + y5;
  30. }
  31. R++;
  32. }
  33. setInterval("flow()", 5);
  34. void(0);
  35. }
  36. </script>
  37. </head>
  38. <body onload = imageFlow()>
  39. <p><img alt="Nature1" src="img1.jpg" /></p>
  40. <p><img alt="Nature2" src="img2.jpg" /></p>
  41. </body>
  42. </html>
Last edited by adaykin : Dec 23rd, 2007 at 8:18 pm.
My Website <-- check out my site!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,851
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 344
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: Function is undefined

  #2  
Dec 24th, 2007
This is because Javascript searches for 'flow()' in the global scope but it so happens that your declaration of flow being present in the function imageFlow is not visible / is not a property of the global object. This can also be attributed to the fact that setTimeout and setInterval always search the global scope.

You can avoid this in many ways, two of which are: Either set this function flow as the property of your function imageFlow or use closures / anonymous functions.

  1. /* Setting the property of imageFlow function */
  2.  
  3. function imageFlow() {
  4. var R = 0, x1 = .1, y1 = .05, x2 = .25, y2 = .24, x3 = 1.6, y3 = .24, x4 = 300, y4 = 200, x5 = 300, y5 = 200;
  5. var DI = document.images, DIL = DI.length;
  6. var flow = function() {
  7. for(var i = 0; i < DIL; i++) {
  8. DIS = DI[i].style;
  9. DIS.position = 'absolute';
  10. DIS.left = Math.sin(R * x1+ i * x2 + x3) * x4 + x5;
  11. DIS.top = Math.cos(R * y1 + i * y2 + y3) * y4 + y5;
  12. }
  13. R++;
  14. }
  15. imageFlow['flow'] = flow;
  16. setTimeout("imageFlow.flow()", 100);
  17. }
  18. window.onload = imageFlow;

  1. function imageFlow() {
  2. var R = 0, x1 = .1, y1 = .05, x2 = .25, y2 = .24, x3 = 1.6, y3 = .24, x4 = 300, y4 = 200, x5 = 300, y5 = 200;
  3. var DI = document.images, DIL = DI.length;
  4. var flow = function() {
  5. for(var i = 0; i < DIL; i++) {
  6. DIS = DI[i].style;
  7. DIS.position = 'absolute';
  8. DIS.left = Math.sin(R * x1+ i * x2 + x3) * x4 + x5;
  9. DIS.top = Math.cos(R * y1 + i * y2 + y3) * y4 + y5;
  10. }
  11. R++;
  12. }
  13. setTimeout(function() { flow(); }, 1000);
  14. }
  15. window.onload = imageFlow;

The second one is definitely a better way since calling setInterval() with a string equivalent of javascript (as in the first one) fires of an instance of interpreter to convert the string to actual javascript code, something not recommended since it is pretty expensive performance wise.
Last edited by ~s.o.s~ : Dec 24th, 2007 at 12:25 am.
I don't accept change. I don't deserve to live.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 11:36 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC