views:

105

answers:

2

i have this function that will shuffle a list of images, when i press on a #shuffle button i want to trigger a message, if is confirmed the second time i will not want to ask again!

how to code this?

$('#shuffle').click(function(){
  if(confirm('This will shuffle all your bla bla bla')){
    $("#upimagesQueue li").shuffle();       
  }
});
+4  A: 

You can use a variable, like this:

var confirmed = false;
$('#shuffle').click(function(){
  if(confirmed || confirm('This will shuffle all your bla bla bla')){
    $("#upimagesQueue li").shuffle();  
    confirmed = true;     
  }
});

This starts with the confirm not being acknowledged, but the first time it is, it's set to true. This means the first part of the condition1 OR condition2 in your if() would be true, so the confirm wouldn't pop up again.

Alternatively, you can use .data() to store the variable for this:

$('#shuffle').click(function(){
  if($(this).data('confirmed') || confirm('This will shuffle all your bla bla bla')){
    $(this).data('confirmed', true);   
    $("#upimagesQueue li").shuffle();   
  }
});
Nick Craver
this is what i seek! thanks!
robertdd
This will work, but it seems a little silly to have extra variables when you can just unbind the event.
SeanJA
@SeanJA - Show me a cleaner/shorter way (with *all* the functionality, like I have above) that does this and I'll concede it's a better option :)
Nick Craver
For the first one, I hate it when the global namespace is full of oneoff variables (maybe that is just me). The data one is quite clever in avoiding that pet peeve of mine.
SeanJA
@SeanJA - Same pet peeve of mine as well, my favorite use is for timeout storage for delayed searches on typing like this: http://stackoverflow.com/questions/2584329/javascript-settimeout-help-needed/2584439#2584439
Nick Craver
A: 

This is a better more readable way

$('#shuffle').bind('click', function(event){
    if(confirm('are you sure?')){
        $(this).unbind(event);
        $('#shuffle').bind('click', function(){
            $("#upimagesQueue li").shuffle();
        });
        $("#upimagesQueue li").shuffle();
    }
});

See: http://api.jquery.com/one/

Could also be written as:

$('#shuffle').bind('click', function(event){
    if(confirm('are you sure?')){
        $(this).unbind(event).click(function(){
            $("#upimagesQueue li").shuffle();
        })
        $("#upimagesQueue li").shuffle();
    }
});
SeanJA
ok... better might be stretching it... another way to do it.
SeanJA
It also needs to run when you click confirm the first time, once you add that in (twice now), I doubt this is "more readable." If however, you bound an event handler that did the confirm (or cancel) first then the shuffling as a separate bind and unbound the first interrupting handler, it might be cleaner...but then you need a named handler so still longer.
Nick Craver
Since it is already being selected, and jQuery returns `this` and .shuffle() is a function, you can chain it
SeanJA
@SeanJA - That would only hold true if `#shuffle` was what was being shuffled :) Check the question again, it's actually shuffling `"#upimagesQueue li"`
Nick Craver
@Nick You got me there, I didn't realise that at first... you win this round sir
SeanJA
@Nick I would rewrite the shuffle function to take a selector... but I think that might be out of scope
SeanJA
@SeanJA - Good show :) Don't get me wrong though - I'm all for being shown a new better, shorter, or easier way I never thought of before...it's one of the best ways to learn/grow in my opinion :)
Nick Craver