JS如何获取调用函数的变量?

时间:2016-10-20 09:10:55

标签: javascript function

我想获取调用函数的变量,我搜索了但我没有找到..

更清楚,

var time = 60

function timer() {
    variableName--
    console.log(variableName)
    return variableName
}

setInterval(() => {
    time = timer()
}, 1000)

所以,我想知道variableName,实际上variableName是时间,所以我可以做

time = timer(time)

但是,它不是很......我不知道,显然有一种方法可以将变量分配给一个函数,(我不知道怎么说,但我希望你理解我了)

所以,我希望我的代码更加清晰,可重复使用, 实际上,我正在编写套接字计时器,

就像:

socket.on('timerStart', () => {
    TIMER = timer()
    console.log(TIMER)
}

function timer() {
    if(variableThatInvoked > 0) {
        variableThatInvoked--
        return variableThatInvoked
    }
}

那么,如何获得variableThatInvoked?

3 个答案:

答案 0 :(得分:0)

您可以尝试这样的事情:

var time = function timer() {
    console.log("time called");
}

以下是使用setTimeout

的异步功能
function async(your_function, callback) {
    setTimeout(function() {
        your_function();
        if (callback) {callback();}
    }, 0);
}


async(time, function() {console.log(time);});

答案 1 :(得分:0)

做像

这样的事情是完全没问题的

var time = 60

function timer(variableName) {
    variableName--
    console.log(variableName)
    return variableName
}

setInterval(() => {
    time = timer(time)
}, 1000)

你可以准确地看到你传递的是什么。

在函数调用time = timer()中,任何时候都没有引用time

答案 2 :(得分:0)

好吧,谢天谢地,我发现它不可行,所以我把它变成了阶级逻辑

所以,对于像我这样的人,有Timer类:

            class Timer {
                constructor(left, rate, func, callback) {
                    this.left = left
                    this.rate = rate
                    this.func = func
                    this.cb = callback
                }

                start() {
                    this.interval = setInterval(() => {
                        this.func()
                        this.left--
                        if(this.left < 0) {clearInterval(this.interval); this.cb()}   
                    }, 1000 / this.rate)
                }
            }

所以,当收到服务器套接字说“嘿嘿”时,让我们开始计时器!&#39;:

            socket.on('startTimer', (servLeft) => {
                timer = new Timer(servLeft, 2, () => {
                    console.log(timer.left)
                    }, () => {console.log('timer ended!')})

                timer.start()
            })

逻辑渲染:

&#13;
&#13;
<!DOCTYPE html>
<html>
    <head>
        <title>Timer</title>
        <script>
            class Timer {
                constructor(left, rate, func, callback) {
                    this.left = left
                    this.rate = rate
                    this.func = func
                    this.cb = callback
                }

                start() {
                    this.interval = setInterval(() => {
                        this.func()
                        this.left--
                        if(this.left < 0) {clearInterval(this.interval); this.cb()}   
                    }, 1000 / this.rate)
                }
            }


            timer = new Timer(20, 1, () => {console.log(timer.left)}, () => {console.log('Timer ended!')})
            timer.start()
        </script>
    </head>

    <body>
        <hi>Simple timer</hi>
    </body>
</html>
&#13;
&#13;
&#13;