473,326 Members | 2,182 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Stop do while loop - image preloader

Hi everyone,

I am trying to stop an image preload sequence by the click of a mouse but
have been unsuccessful trying several methods. Imagine this simple script
below that loads 50 images to cache. If the stopPreload() function is
activated and the ret val set to false, the preload() function still
continues to the end.

Any suggestions on how to stop the preload() function in its process, what
conditions are necessary?

var ret=true;
function preload(){
var k=1;
do {
theImageSrc="image"+k+".jpg";
document.imgArry[k]=new Image;
document.imgArry[k].src=theImageSrc;
k++;
}
while (ret==true && k < 50)
}
}

function stopPreload(){
ret=false;
}

Thanks, David
Aug 10 '05 #1
4 3068
If I understand you correctly, you want to be able to stop the loop in
response to an onclick event.

As far as I understand things (others will no doubt correct me/express
it more accurately) as a general rule, once a function starts running,
the browser window will not respond to any further events until the
function has completed and returned.

Accordingly you cannot (without some fancy foot-work) generally stop a
function by means of a user onclick event once it has begun.

When you click to change ret val, the stopPreload function does not
actually run until preLoad has finished.

The only way I have found (as an amateur) to change this is quite
complicated, and is based on the fact that whilst one "window" may be
unresponsive, others will not be.

1. Make "ret" the property of an object.

var oRet={"ret":true};

while (oReg.ret==true)

2. Before commencing preLoad, open up a dialogue box or iframe (i.e.
a new "window"), which contains the "cancel" button, and pass the oRet
object as a reference.

3. The new window object will detect the onclick event, and can
update the oRet object, which then feeds back to the preLoad function.

I have used this approach before to create progress bars with cancel
buttons as separate dialogue boxes.


2.

Aug 10 '05 #2

"Baconbutty" <ju****@baconbutty.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
If I understand you correctly, you want to be able to stop the loop in
response to an onclick event.

As far as I understand things (others will no doubt correct me/express
it more accurately) as a general rule, once a function starts running,
the browser window will not respond to any further events until the
function has completed and returned.

Accordingly you cannot (without some fancy foot-work) generally stop a
function by means of a user onclick event once it has begun.

When you click to change ret val, the stopPreload function does not
actually run until preLoad has finished.

The only way I have found (as an amateur) to change this is quite
complicated, and is based on the fact that whilst one "window" may be
unresponsive, others will not be.

1. Make "ret" the property of an object.

var oRet={"ret":true};

while (oReg.ret==true)

2. Before commencing preLoad, open up a dialogue box or iframe (i.e.
a new "window"), which contains the "cancel" button, and pass the oRet
object as a reference.

3. The new window object will detect the onclick event, and can
update the oRet object, which then feeds back to the preLoad function.

I have used this approach before to create progress bars with cancel
buttons as separate dialogue boxes.

That's what I thought about the function prosess not being able to be
interupted from another function. At least that's my experience in trying to
get it to stop. Your suggestion sounds good but unfortunately opening a new
"window" isn't an option.

Thanks for the suggestion, David

Aug 10 '05 #3
As already explained, you are in a tight loop, it is unlikely the
browser will respond to any events while executing this code. There is
also a matter of whether JavaScript is multithreaded and can actually
run the stopPreload() function while preload() is executing.

You should be able to make this work, but it will require the use of
setTimeout() or setInterval(), which will slow down your preload
sequence.

var preloadTimer= setInterval(preload, 10);
function preload() {
if ('undefined' == typeof loop) {
loop = 0;
}
document.imgArry[loop] = new Image();
document.imgArry[loop].src = "image" + loop + ".jpg";
if (++loop >= 50) {
clearInterval(preloadTimer);
}
}
function stopPreload() {
if (preloadTimer) {
clearInterval(preloadTimer);
}
}
Aug 10 '05 #4
> As already explained, you are in a tight loop, it is unlikely the
browser will respond to any events while executing this code. There is
also a matter of whether JavaScript is multithreaded and can actually
run the stopPreload() function while preload() is executing.

You should be able to make this work, but it will require the use of
setTimeout() or setInterval(), which will slow down your preload
sequence.


Grant,

Thanks, this is interesting. I don't mind a little drag on the preload, just
need to 'pause' or stop it so that I can load an image, that is currently
preloading, that has not yet cached. Otherwise, the image I am trying to
display has to wait until it has been cached by the preloader.

Once this image has been displayed "instantly" or the time it would take to
cache this image only, I can resume the preload, or start it over again.

I'll chew on your example. It looks promising.

David
Aug 10 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Magic-chef | last post by:
This rollover script is created by Image Reaady. When using IE and especially with Win XP the images involved in the rollover disappear. It happens gradually with one or two at first then all. ...
16
by: deko | last post by:
I have a sub routine that searches the Outlook PST for a message sent to/from a particular email address. Obviously, this can take a long time when the PST contains thousands of messages. I'd...
14
by: S. Nurbe | last post by:
Hi, I have programmed a while loop. At the end of this loop I want to stop it until someone hit a key. When someone hits the right key the loop shall start again. Actually I thought this would...
2
by: sean.f.duffy | last post by:
Hi! I am an effectivebrand.com toolbar user and would love to add a preloader to my toolbar, but am having problems doing this. Is it possible to have javascript look at the URL of a webpage,...
3
by: abm | last post by:
Hi. I am in great need of a preloader for my Flash web site. I have looked at a lot of tutorials and examples, but I can not seem to get it to work. How do I add a preloader to my existing Flash...
2
by: bedges | last post by:
okay, the scenario: i have a header image which changes randomly across all pages in the site. that works fine. i also have an image preloader within the random header picker which theoretically...
2
by: streammalvern | last post by:
Hi, I am having a problem with a published Flash Movie. I included a preloader and the whole file size of the .swf is only about 1MB. If I go to the movie, it pauses even before the preloader...
0
by: vinayakk | last post by:
Hello webies, I have a problem with loading html pages in frameset. I am using location.href to load html pages one by one upon the user request and these pages in turn load flash files. When one...
8
donilourdu
by: donilourdu | last post by:
hi, I want to preload the image on page load using javascript by, Doni
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.