sepoto Posted June 1, 2011 Share Posted June 1, 2011 I have tried to call scrollBg() with two different functions setTimeout and setInterval none of which are doing anything at all for my cause. I was wondering if anyone might know what is going on here. <script type="text/javascript"> $(document).ready(function(){ var xsrc = $('#cityscape').attr('src'); if(xsrc == 'img/cityscape01.jpg') { $('#cityscape').attr({src: 'img/cityscape02.jpg', alt: 'cover2'}); } alert('Changed Image'); function scrollBg(){ alert('msg'); var xsrc = $('#cityscape').attr('src'); if(xsrc == 'img/cityscape02.jpg') { $('#cityscape').attr({src: 'img/cityscape01.jpg', alt: 'cover2'}); } } var init = setInterval("scrollBg()", 20000); setTimeout('scrollBg()', 5000);}); </script> Link to comment Share on other sites More sharing options...
justsomeguy Posted June 1, 2011 Share Posted June 1, 2011 Is the function running, and are there error messages? Link to comment Share on other sites More sharing options...
sepoto Posted June 1, 2011 Author Share Posted June 1, 2011 alert('Changed Image'); is called. The second alert inside scrollBg is never called. I tried to get the function to call with setIntercal and setTimeout to no avail yet. It is strange I think. Link to comment Share on other sites More sharing options...
thescientist Posted June 1, 2011 Share Posted June 1, 2011 no errors? what's the HTML/CSS code that goes along with that, so we can try it ourselves. Link to comment Share on other sites More sharing options...
jeffman Posted June 1, 2011 Share Posted June 1, 2011 Alert the value of xsrc. It is possible the browser is expanding the relative path to an absolute path. If that's the case, you can test the value with xsrc.match instead of a simple comparison. Link to comment Share on other sites More sharing options...
thescientist Posted June 1, 2011 Share Posted June 1, 2011 Alert the value of xsrc. It is possible the browser is expanding the relative path to an absolute path. If that's the case, you can test the value with xsrc.match instead of a simple comparison.but wouldn't alert('msg') execute regardless of the outcome of the following conditional? If the function is never run, as the OP is indicating since he never see's that particular alert, it is safe to say the script is never even making it that far in the first place.I'd still like to know if the OP is checking for errors, and if so, what are they. Link to comment Share on other sites More sharing options...
jeffman Posted June 1, 2011 Share Posted June 1, 2011 Yeah. I think we have a scope issue. setInterval and setTimeout should call scrollBg by reference (creating a closure), not in a quoted string, where it won't be recognized.I'm sure the error message says something like "scrollBg not found." Link to comment Share on other sites More sharing options...
thescientist Posted June 1, 2011 Share Posted June 1, 2011 That makes sense, since window is considered the highest level global object, and in this case scrollBg is in Document's scope. Link to comment Share on other sites More sharing options...
sepoto Posted June 1, 2011 Author Share Posted June 1, 2011 Yeah. I think we have a scope issue. setInterval and setTimeout should call scrollBg by reference (creating a closure), not in a quoted string, where it won't be recognized.I'm sure the error message says something like "scrollBg not found."That is exactly what it was. A scope issue. This is what the could ended up looking like:<script type="text/javascript" src="jquery/jquery-1.6.1.min.js"></script><script type="text/javascript"> function scrollBg(){ var xsrc = $('#cityscape').attr('src'); if(xsrc == 'img/cityscape01.jpg') { $('#cityscape').attr({src: 'img/cityscape02.jpg', alt: 'cover2'}); } else if(xsrc == 'img/cityscape02.jpg') { $('#cityscape').attr({src: 'img/cityscape03.jpg', alt: 'cover2'}); } else if(xsrc == 'img/cityscape03.jpg') { $('#cityscape').attr({src: 'img/cityscape04.jpg', alt: 'cover2'}); } else { $('#cityscape').attr({src: 'img/cityscape01.jpg', alt: 'cover2'}); } } var init = setInterval("scrollBg()", 20000);</script> Link to comment Share on other sites More sharing options...
Ingolme Posted June 3, 2011 Share Posted June 3, 2011 The solution is simple: Don't evaluate a string, reference the function instead.Wrong:var init = setInterval("scrollBg()", 20000);Right:var init = setInterval(scrollBg, 20000);It would have been better if the setTimeout and setInterval functions had never accepted strings as an argument to begin with. Link to comment Share on other sites More sharing options...
sepoto Posted June 3, 2011 Author Share Posted June 3, 2011 That is a wonderful pearl of wisdom. Thank you. Link to comment Share on other sites More sharing options...
thescientist Posted June 3, 2011 Share Posted June 3, 2011 It would have been better if the setTimeout and setInterval functions had never accepted strings as an argument to begin with.good call on that one. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.