Why does the picture change when you click on the text of the an attribute?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <script>
    var n = 1;
    function changePic(m){
        return n = m;
    }

    function change(){
        var myImg = document.getElementsByTagName("img")[0];
        myImg.src = "images/0" + n + ".jpg";
        if(n<5)nPP;
        else n=1;
    }
    
    </script>
</head>
<body onload="setInterval(change,2000);">
    <img src="images/01.jpg" alt="" width="200">
    <div>
        <a href="-sharp" onclick="changePic(1)"></a>
        <a href="-sharp" onclick="changePic(2)"></a>
        <a href="-sharp" onclick="changePic(3)"></a>
        <a href="-sharp" onclick="changePic(4)"></a>
        <a href="-sharp" onclick="changePic(5)"></a>
    </div>
</body>
</html>

the above code is very simple, the picture every 2 seconds, change, this I understand.

the question is, when I click on the properties of a, such as travel, why does the picture jump to the travel picture?
changePic only returns the number 2 and does not see that it will change the picture?
change and changePic are two functions, and changePic cannot call the change function, huh?

Apr.11,2021

n is a global variable.

changePic doesn't modify the picture, he just modifies n .

in change , the logic is: replace the picture number with n, and then change n to the next number.
well, before the next change executes, changePic modifies the value of n in advance, causing change to show the number clicked.
this n is passed in when you click.

so this is a problem of using global variables. changePic didn't change the picture, just a variable that describes the number of the next picture.

Click did not immediately change the picture, or wait until Interval two seconds to change the picture through change to change the picture

Menu