How to get variables in another function from one function in JavaScript

the code is as follows

$(function(){
    $(".a").on("click",function(){
        var counter=0
        counterPP
    })
    
    $(".b").on("click",function(){
        if(counter>=3){
            alert(3)
        }
    })
})

there are two buttons, an and b, click a, and the variable counter will increase by 1. When the b button is clicked, a prompt box will pop up if the variable counter is greater than or equal to 3.
because counter is not a global variable, it will be destroyed after each click, so every time counter is 1, how to implement it if you use a closure. If the global variable is no longer used, how should the countervalue in the a button function be obtained in the function clicked by the b button?

Feb.28,2021

must be declared in the upper scope, such as this

$(function(){

    (function() {
        var counter = 0;
        
        $('.a').on('click',function() {
            counterPP;
        });
        
        $('.b').on('click',function() {
            if(counter > 3) {
                alert('3');
            }
        });
    })();
})

because you wrap a layer with an anonymous function, the local variables defined in this anonymous function are valid in the child function, that is, you should:

$(function(){
    var counter=0;
    $('.a').on('click',function(){
        counterPP;
    })
    
    $('.b').on('click',function(){
        if(counter>=3){
            alert(3);
        }
    })
})

your original listening function logic for a will cause counter to always be 1, because every time you enter it, you initialize it to 0 and then add 1.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1eaeda9-5d027.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1eaeda9-5d027.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?