May 4, 201016 yr Hello can anyone tell me why an onload function won't work where an onclick function will? <p onload="getRequest( 'getOffers.php', updateTarget )"> Click Here. </p> <div id="target"> </div> <br /> From here I wish to use ajax to automatically populate special offers every 20 seconds randomly switching through them. <p onclick="getRequest( 'getOffers.php', updateTarget )"> This is the working code I am using right now which randomly switches through special offers on user click. My goal is to remove the need of the user click, instead switch every 20 seconds. A point in the right direction would be greatly appreciated. Please reply if you need more code to help with this query.
May 4, 201016 yr The problem is that P tags don't allow the onload method http://www.w3.org/TR...xt.html#h-9.3.1 If memory serves, only the <body> tag allows it
May 4, 201016 yr I read this on the internet somewhere... this any help at all? function foo() { // do stuff // ... // make a delayed recursive call to foo setTimeout(foo, 3000); }
May 4, 201016 yr I would highly recommend you take a look at jQuery. pretty sure you could implement this with it no problem Some fantastic video tutorials can be found here I would suggest you watch them all. They may take a while to learn initially but they will probably give you inspiration for more interactivity in the future as well as solving this problem
May 4, 201016 yr @epic - I'm pretty sure you only need to define a timeout once for it to repeat itself. I think that function might end up being called increasingly more with each 3 second gap (exponentially more actually). I could however be wrong on this
May 5, 201016 yr Author I think I am responsible for the recent lack of fluffy white kittens then. Thanks Jay but it is an assignment and the requirement is to use self written ajax to produce the desired result. I just have little idea of how to go about this. I am actually looking at your website now at the timer class. Is this the sort of thing that could be adapted to meet my requirements?
May 5, 201016 yr Hi OllieJay, No, the timer class I wrote is for PHP not JavaScript, it doesn't work client side only server side Take a look at http://www.developermonkey.com/tutorials for some great ajax/javascript tutorials
May 6, 201016 yr Author Right after a lot of trial and error I have my special offers automatically and randomly loading, however it switches every half second, I have tried to alter the code to affect the timer to no avail. I assume I need some sort of timer delay? maybe another function? any idea? Here's the javascript that affects the population: function updateTarget( txt ) { document.getElementById('target').innerHTML = txt; } function commence(updateTarget) { timer = setInterval("getRequest('getOffers.php', updateTarget);", 100); } var timer = 0;
May 6, 201016 yr change the 100. Thats milliseconds, so its 0.1 seconds, 20 seconds is 20000, so that should be the value
May 6, 201016 yr Author ahh trés bien. Can't thank you enough, gonna have to put you on my references. +1 for all your posts.
May 6, 201016 yr Author Oh, final question. The 20 second switch works fine although the first special offer takes 20 seconds to appear is there any way I can get one to appear on load before the switcher starts?
May 6, 201016 yr You can call the function directly using window.onload = getRequest('getOffers.php', updateTarget); I think - I'm not really a javascript person tbh
Create an account or sign in to comment