Cannot get parameters in function

<script>
    let step = 10, //
        index = 0, //
        flag = -1, //
        dis = 0, //
        width = 400, //
        num = 5; //
    const pics = ["../one.jpg", "../two.jpg", "../three.jpg", "../four.jpg", "../five.jpg"];
    let wrapper = document.querySelector(".wrapper");
    let menu = document.querySelector(".menu");
    //
    pics.forEach(val => {
        let img = document.createElement("img");
        let smallImg = document.createElement("img");
        img.src = val;
        smallImg.src = val;
        wrapper.appendChild(img);
        menu.appendChild(smallImg);
    });

    //
    function addPic() {
        pics.forEach(val => {
            let img = document.createElement("img");
            img.src = val;
            wrapper.appendChild(img);
        });
    }
    //style
    function getStyle(ele, cssname) {
        if (window.getComputedStyle) {
            return window.getComputedStyle(ele)[cssname];
        } else {
            return ele.currentStyle[cssname];
        }
    }
    //
    function pace() {
        let curPos = parseInt(getStyle(wrapper, "left"));
        console.log("pace");
        console.log(to);
        console.log(curPos);
        wrapper.style.left = curPos + flag * step + "px";
        dis += flag * step;
        //
        if (Math.abs(to - dis) < step) {
            wrapper.style.left = to + "px";
            dis = to;
            indexPP;
            clearInterval(pacer);
            // setTimeout(() => {
            //     timer = setInterval(pace, 100);
            // }, 2000);
        } else {
            wrapper.style.left = curPos + flag * step + "px";
            dis += flag * step;
        }
    }
    //
    function move(direction) {
        let to = -width * index; // 
        // -1-left 1-right
        flag = direction === 1 ? 1 : -1;
        console.log(to);
        console.log(dis);
        pacer = setInterval(pace, 100);
    }

    //
    function auto() {
        addPic();
        indexPP;
        move(flag);
    }
    auto();
</script>

browsers keep prompting
Uncaught ReferenceError: to is not defined at pace

but isn"t to defined in move ()?

I don"t know what"s wrong with it. Ask the bosses for advice.

Jul.17,2022

emmmm, it is recommended to read the differences between let, const and var in the es6 documentation.


usually const declares a read-only constant, and variables declared by let are valid only at the block level. You can take a look at es6's variable declaration rule http://es6.ruanyifeng.com/-sharpdo...


let a = '123'
function move(direction) {
    let to = 'xxx' // to == 'xxx'
    console.log(to) // xxx
    console.log(a) // 123
}
function A(){
    console.log(to) // undefined
    console.log(a) // 123
}
console.log(to) // undefined
console.log(a) // 123

let is only available within the defined range, which is probably the part framed by {}


should be a scope problem. Move and pace are declared in the same scope, while variables in pace can only be found in this scope or up.

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-7b5f91-29a4b.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-7b5f91-29a4b.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?