函数内部的 setTimeout 调用函数 - Scope-Issue [英] setTimeout calling function inside function - Scope-Issue

查看:39
本文介绍了函数内部的 setTimeout 调用函数 - Scope-Issue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,问题是我在需要由 setTimeout 调用的函数中有一个函数.然而,这不起作用,因为 setTimeout 会假设它调用的函数将根作为其作用域.

So, the problem is that I have a function inside a function that needs to be called by setTimeout. That doesn't work however because setTimeout will assume that the function it calls has the root as its' scope.

知道如何在不改变函数范围的情况下解决这个问题吗?

Any idea how I could solve this without changing the scope of the function?

我的意思是:

function general(){
    function saysomething(){
        console.log('hi there');
    }
setTimeout("saysomething();", 1000);
}

setTimeout 失败..

The setTimeout fails..

推荐答案

function general(){
    function saysomething(){
        console.log('hi there');
    }
    setTimeout(saysomething, 1000);
}

这篇关于函数内部的 setTimeout 调用函数 - Scope-Issue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆