I personally like to use timestamps over ints or rand, a timestamp is ensured to be unique. If you use an int and in this session you had "http://.....gif?1", the next time the user goes into the app, they re-use the integer 1, using the cache. And Math.random isn't as random as you want it to be: http://stackoverflow.com/questions/1062902/how-random-is-javascripts-math-random. But a timestamp, that is going to be unique unless you reset your system's clock.
And performance wise, trying to optimize this is a premature-optimization. Unless your request is called many many thousands of times a second, the performance overhead of using a timestamp will be effectively 0. Check out: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getTime, this returns the number of milliseconds since Jan 1, 1970, again, unless the user resets their system clock, it is guaranteed to be unique. try requesting: "http://....gif?" + ( new Date() ).getTime(); On Mon, Dec 20, 2010 at 9:45 AM, Loic Giraudel <[email protected]> wrote: > Or for better performance a simple incremented int variable. > > > On Mon, Dec 20, 2010 at 10:43, fernando trasvina <[email protected]>wrote: > >> use ?Math.random() on ur uri >> or timestamp + Math.random() >> >> On Dec 20, 2010, at 3:35 AM, Amit Agarwal wrote: >> >> Hi every one, >> >> I need to poll an image using javascript and need to perform an action >> once the image is found at its position. This is the code I have written for >> this task. >> >> /*----Image handling script starts here----*/ >> var beacon = new Image(); >> >> >> >> beacon.onload = function() { >> >> >> console.log('Image found'); >> console.log(this.width,this.height); >> >> >> >> window.clearInterval(timer); >> }; >> >> >> beacon.onerror = function(){ >> console.log('Image not found'); >> >> >> } >> var timer = window.setInterval(function(){ >> >> >> console.log('sending the request again'); >> beacon.src = "http://www.google.co.in/logos/2010/lennon10-hp.gif"; >> >> >> },2000); >> /*----Image handling script ends here----*/ >> >> Problem is that, after one GET request, the response gets cached and >> requests don't get sent everytime I set src. If you examine NET tab, it >> sends request only on first src set and caches the response. >> >> I need to send a fresh request for image every time my code sets the src. >> Any workarounds? >> >> -Regards >> *Amit Agarwal <http:/www.amitagrwal.com>* >> +91-779-822-8765 >> >> >> -- >> To view archived discussions from the original JSMentors Mailman list: >> http://www.mail-archive.com/[email protected]/ >> >> To search via a non-Google archive, visit here: >> http://www.mail-archive.com/[email protected]/ >> >> To unsubscribe from this group, send email to >> [email protected] >> >> >> -- >> To view archived discussions from the original JSMentors Mailman list: >> http://www.mail-archive.com/[email protected]/ >> >> To search via a non-Google archive, visit here: >> http://www.mail-archive.com/[email protected]/ >> >> To unsubscribe from this group, send email to >> [email protected]<jsmentors%[email protected]> >> > > -- > To view archived discussions from the original JSMentors Mailman list: > http://www.mail-archive.com/[email protected]/ > > To search via a non-Google archive, visit here: > http://www.mail-archive.com/[email protected]/ > > To unsubscribe from this group, send email to > [email protected]<jsmentors%[email protected]> > -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/[email protected]/ To search via a non-Google archive, visit here: http://www.mail-archive.com/[email protected]/ To unsubscribe from this group, send email to [email protected]
