Control value of a Javascript variable

0

I have the following code, where there are 2 functions of draggable / droppable.

For each element that is placed well, I increase my variable by 1 to know that all the elements were placed well.

I did another function where I control that variable that I add, to say that if this variable is 2, I do something else ...

But this is not good for me (the IF of the 2nd script does not run), I need your help.

This script controls the placed elements and incremeta the 'totalShadows' var:

<script>
    $(function () {
        var totalSombras = 0;

        $('#silueta1').droppable({
            tolerance: 'fit',
            hoverClass: 'active',
            accept: "#objeto1",
            drop: function(e, ui) {
                $(this).html(ui.draggable.remove().html());
                $(this).droppable('destroy');
                $( this )
                //.addClass( "ui-state-highlight" )
                .attr("src", "images/jirafa.png");
                var thissound = document.getElementById('colocarSilueta');
                thissound.volume = 0.2;
                thissound.play();
                totalSombras = totalSombras + 1;
                contar(totalSombras);
            }
        });

        $('#silueta2').droppable({
            tolerance: 'fit',
            hoverClass: 'active',
            accept: "#objeto2",
            drop: function(e, ui) {
                $(this).html(ui.draggable.remove().html());
                $(this).droppable('destroy');
                $( this )
                .attr("src", "images/vaca.png");
                var thissound = document.getElementById('colocarSilueta');
                thissound.volume = 0.2;
                thissound.play();
                totalSombras = totalSombras + 1;
                contar(totalSombras);
            }
        });
    });
</script>

This script controls whether the 'totalShadows' var is worth 2:

<script>
    jQuery(function contar(cont) {
        if (cont == 2) {
            alert("asd");
        }
    })
</script>
    
asked by Oren Diaz 20.10.2017 в 02:58
source

1 answer

1

You must declare the variable outside the function, example:

<script>
    var totalSombras = 0;
    $(function () {
        $('#silueta1').droppable({
    ...

And the count function, you should not create it with jquery, only the function as such:

<script>
    function contar(cont) {
        if (cont == 2) {
            alert("asd");
        }
    }
</script>
    
answered by 20.10.2017 / 03:47
source