473,545 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="im age"+k+".jpg";
document.imgArr y[k]=new Image;
document.imgArr y[k].src=theImageSr c;
k++;
}
while (ret==true && k < 50)
}
}

function stopPreload(){
ret=false;
}

Thanks, David
Aug 10 '05 #1
4 3092
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":tru e};

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****@baconbu tty.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.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":tru e};

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(pre load, 10);
function preload() {
if ('undefined' == typeof loop) {
loop = 0;
}
document.imgArr y[loop] = new Image();
document.imgArr y[loop].src = "image" + loop + ".jpg";
if (++loop >= 50) {
clearInterval(p reloadTimer);
}
}
function stopPreload() {
if (preloadTimer) {
clearInterval(p reloadTimer);
}
}
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
5072
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. Even a browser refresh does not bring the images back. I there a problem known issue with Image Ready rollover scripts and IE+XP? <script...
16
12644
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 like to put a button on my Access 20003 form that will stop the sub routine when clicked. I know I can press Ctl + Break and stop the code, but is...
14
1624
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 be easy to do, but when it waits for input the application apparently works in the background and when I hit the right key the loop was already...
2
2040
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, then follow links on that page, looking for images within that website (say one page deep). Once found, could the images be preloaded into the...
3
7944
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 app??? (if you look at www.abmpic.com you will see that it takes too long to load without a preloader)
2
2056
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 loads the header image before the rest of the graphical content on the page. however, what appears to be happening, judging by the status bar...
2
1907
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 appears on the screen. I have the preloader located on frame 1 & 2 of the first scene. When I test the movie out of Flash and I view bandwidth and...
0
2765
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 html page is replaced by another it takes some time to load but in the mean while a white screen appears and the whole thing appears mess. Can this...
8
2190
donilourdu
by: donilourdu | last post by:
hi, I want to preload the image on page load using javascript by, Doni
0
7411
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7669
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7926
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7439
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7773
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5987
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4962
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3468
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
722
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.