简体   繁体   中英

How to compare two different values of different functions or same function?

I have two variables index_1 and index_2 of different function and I have to compare that. Is that correct method or I can something else

Currently, I am not satisfied with this code for comparison because every time i get same value:

$(document).ready(function(){
       var index_1 = '';
       var index_2 = '';

        $('#spin-now').click(function(){

        $(".spin-box-1").jCarouselLite({
            auto: 200,
            speed: 200,
            visible: 1,
            vertical: true,
            easing: "easeOutBounce",
                afterEnd: function(a) {
                     var index_1 = $(a[0]).index();
                     store_index_1(index_1);    
                }
        });

        $(".spin-box-2").jCarouselLite({
            auto: 225,
            speed: 225,
            visible: 1,
            vertical: true,
            easing: "easeOutBounce",
                afterEnd: function(a) {
                     var index_2 = $(a[0]).index();
                     store_index_1(index_2);
                }
        });       

    });

            function store_index_1(x){
                data_index_1 = x;
                data_index_2 = data_index_1 ;
                if(data_index_2 == data_index_1){
                    //alert('same');
                }
               else{
                  alert('different');
                }


                  console.log('outside'+''+data_index_1+''+data_index_2);  
            }

//console.log(data_index_1);


});

(I think this should be a comment, but I don't have enough reputation)

You defined 2 global variables, so if you're going to use them, then save your first index of .spin-box-1 in index_1 and .spin-box-2 in index_2 , and then call a ' compare ' function (your store_index_1 ).

$(document).ready(function(){
       var index_1 = '';
       var index_2 = '';

        $('#spin-now').click(function(){

        $(".spin-box-1").jCarouselLite({
            auto: 200,
            speed: 200,
            visible: 1,
            vertical: true,
            easing: "easeOutBounce",
                afterEnd: function(a) {
                     index_1 = $(a[0]).index(); //index_1 is your global
                }
        });

        $(".spin-box-2").jCarouselLite({
            auto: 225,
            speed: 225,
            visible: 1,
            vertical: true,
            easing: "easeOutBounce",
                afterEnd: function(a) {
                     index_2 = $(a[0]).index(); //index_2 is your global
                }
        }); 
        compare(); //index_1 and index2 should be set by this line.

    });

            function compare(){
                if(index_1 == index_2){
                    alert('same');
                }
               else{
                  alert('different');
                }

                  console.log('outside'+''+index_1+''+index_2);  
            }

//rest of your code 

});

The thing with your store_index_1(x) function is that it will always be true because you are comparing 2 equal values:

data_index_2 = data_index_1 ;
if(data_index_2 == data_index_1)

That's kind of nonsense so i'm confused about what are you trying to do. And again, sorry for the 'answer' 'cause I can't comment yet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM