문제

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();       
  }
});
도움이 되었습니까?

해결책

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();   
  }
});

다른 팁

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();
    }
});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top