Dev0ps
1 year ago

how to start countdown for two variables at a time

Posted 1 year ago by Dev0ps

I have a function which is working fine with one variable but when I pass two values one by one its countdown for value one and then start the countdown for value two.

I want to start counting for each value as soon as a value comes in it.

ex A and B are two vales

A is running

1, 2, 3

and in the meantime, B comes so it not interrupt A and start from 1 for B.

A 4, 5.......... B 1, 2, 3.............

Please help

  isLocked = false;
    isActive = null;
    var i = 0, max = 10, cnt = 1;
    timer = function(query) {
        i += cnt;
        // if (i>=max) {cnt = -1;}
        // if (i===0)  {cnt = 1;}
        console.log(i);
        if(i == 10){console.log('done');
            $('#1').slideToggle();
            i=0;
            alert(query);
            isLocked = false;
            return;
        }
        setTimeout(timer,1000);
    }

    $(function() {
        var $appeared = $('#appeared');
        var $disappeared = $('#disappeared');

        $('section h3').appear();
        $('#force').on('click', function() {
            $.force_appear();
        });

        $(document.body).on('appear', 'section h3', function(e, $affected) {
            $appeared.empty();
            $affected.each(function() {

                if(isActive == this.innerHTML)
                {
                    return false;
                }
                isComming = this.innerHTML;
                console.log(isComming);
                if(isComming == this.innerHTML)
                {
                    console.log('there');
                    isActive = this.innerHTML;
                    if(isLocked == false){
                        console.log('there');
                        isLocked = true;
                        timer(isActive);
                    }
                }
                // else if(isActive != this.innerHTML){
                //     console.log('else');
                //     timer(isComming);
                // }


                $appeared.append(this.innerHTML+"\n");
            })
        });

        $(document.body).on('disappear', 'section h3', function(e, $affected) {
            // this code is executed for each disappeared element
            $disappeared.empty();
            $affected.each(function() {
                $disappeared.append(this.innerHTML+"\n");
            })
        });
    });

Please sign in or create an account to participate in this conversation.