Tag Archives: break

jQuery: How to Break Out of an Each Loop Programming 16 MAR 2015

The ever so useful jQuery javascript library has a homegrown loop function entitled .each(), which allows you to iterate over a jQuery object, and at the same time execute a function for each matched element.

Very useful indeed.

However, sometimes you might need to break out of a loop early (for example, maybe your loop has the potential of carrying on forever – which obviously is not so great for your patiently waiting audience) – and this is how you do it:

To break a $.each() loop, you have to return false in the loop callback function. (Returning true is equivalent to continue in a normal loop, in other words it skips to the next iteration – exactly the opposite of what you are trying to achieve!)

An example:

$(document).ready(function(){
$('.lotsOfTheseClassesExist').each(function(i, item){
alert($(item).prop('id'));
return false; //exits the loop
});
});

So your break statement for a jQuery each() loop is literally return false.

girl in pink high heels and a black leotard with lots and lots of shiny hula hoops

jquery logo

Related Link: http://api.jquery.com/each/

Feeding the Strand Duck Pond Geese (2015-03-07) Photo Gallery 09 MAR 2015

We kicked off an incredibly busy weekend (more on this at a later stage) by heading to the family favourite Strand duck pond (just across Strand High School) in order to feed all the hungry quackers. Armed with a loaf of fresh white bread purchased on the way at Cinnamon Square Spar, we arrived at the duck pond only to discover Emily fast asleep in her Bambino car seat.

Not wanting to wake her seeing as she needed the sleep having woken up particularly early on this Saturday morning, we left the windows wound down and stepped out onto the grass embankments – and were immediately flocked by dozens of geese and rather cute white ducks.

(For a change, Egyptian Geese didn’t dominate the scene. Also, the duck pond was at a mysteriously low level, certainly much lower than what I’ve seen it before. Not sure whether maintenance, leak, or just old fashioned Summer evaporation.)

IMG_20150307_113329

Jessica quickly clambered up onto a park bench and started dishing out white bread slices left, right and center, and it wasn’t long before Chantelle and I found ourselves also joining in on the fun!

Lots of quacking, flapping and bread devouring was to be had, and no time at all the bread was done and we could make a run for it, back to the safety of the car and ready to head on to the next stop of our jam-packed family activities weekend!

Nail Clippers No More My Life 23 AUG 2011

Ah ha! I knew that my newly reinvigorated training regime would pay off. Only a few days back in, and already I have shown my remarkably increased strength by callously snapping my nail clippers’ compound lever right off the device whilst in the process of clipping my fingernails this weekend!

Ryan can attest to this seeing as I was speaking on the phone (via speaker phone) to him when this calamity occurred.

I need to be more careful in the future now. Don’t know the extent of my own strength any more…. handing out hugs might be dangerous from now on in.

jQuery Draggable, Droppable and Sortable User Interface not working in Internet Explorer 9 (IE 9) CodeUnit 18 APR 2011

I received a bit of a nasty shock when by sheer chance, one of the project managers updated their machines and got Internet Explorer 9 installed in the process, only to instantly come back to me and announce that the drag and drop features on our web project no longer worked – at all.

A quick look around, and indeed all the draggable, droppable and sortable interfaces that come with our use of the awesome jQuery and jQuery UI libraries were all of a sudden not working under IE 9. The other browsers, IE 8 included, still worked fine, but not this new beast of a browser.

Thankfully though, a quick look about on the jQuery UI support forums yielded the fact that a) this was indeed a bug that has long been identified, and b) that the bug fix ticket around it has since been resolved and closed.

See http://forum.jquery.com/topic/jquery-ui-does-not-work-on-ie9 .

So how do you sort out the problem then? Well if you can, you need to upgrade to the latest version of the jQuery UI library. The fix has now been in since version 1.8.6, so anything higher will resolve the issue.

And now for a big sigh of relief :)

Related Link: http://forum.jquery.com/topic/jquery-ui-does-not-work-on-ie9

…and I’m Back General Nonsense 13 DEC 2010

Whew, okay, I’m back to wreak more havoc with the written word, having taken a bit of a break from blogging thanks to a) the really enjoyable X-Men Origins: Wolverine xbox game, b) the really enjoyable Marvel Anime: Iron Man, c) the really enjoyable Wolverine and the X-Men animated show, d) the NOT so enjoyable experience of having my blogs hacked and having to clean up and restore everything, e) loads of fun family visits, f) the NOT so fun daily extended commutes, and finally the most IMPORTANT reason in explaining away my tardiness in these pages:

Jessica, already having had enough of her dad's incessant prattle!

Continue reading

PHP: Exit a While or For Loop with Break Programming 31 JAN 2010

How do you exit or break out of a running loop before it has finished completing in PHP?

Well luckily PHP makes it pretty easy by providing us with the break control structure, a function that forces the ending of the execution of the current for, foreach, while, do-while, or switch structure.

You can extend break‘s range by utilizing its single numeric argument that tells it how many nested enclosing structures need to in fact be broken out of.

In other words, a bloody useful function to know! :)

Example Usage:

while(true){
echo date('Y/m/d H:i:s') . '<br />';
break;
}

The code above launches an infinite continuing loop that will print out the current time until the application is eventually forced closed. However, by inserting the break command, the date only gets echoed out once before the loop is forcefully terminated.

If this while loop had been contained within another while loop, the code as it stands would simply break out of the first while loop but continue to execute as part of the top while loop – however, if we specify break 2, the application would forcefully exit both while loops and thus finish execution.

Related Link: http://php.net/manual/en/control-structures.break.php