简体   繁体   English

从JavaScript中的其他命名函数调用命名函数

[英]Calling named function from other named function in javascript

I have run into a problem with a named function in Javascript. 我在Javascript中遇到了命名函数的问题。

I have this reload function 我有这个重装功能

   SN.Reload = function(settings) {

        var _timer = null;
        var $grid = null;

        var init = function () {

            $grid = $(settings.wrapperSelector);

            if (_timer > 0 || _timer != null)
                _timer = settings.timer;
            else
                _timer = 600000;

            window.setInterval(function () {

                LoadData();

            }, _timer);

        };

        var LoadData = function () {

            $.ajax({
                url: '/data.json',
                type: 'GET',
                dataType: 'json',
                cache: false,
                success: UpdateData,
                error: DataErrorHandler
            });

        };
}

In the normal state this will run LoadData function at X minutes - this works as intended. 在正常状态下,它将在X分钟时运行LoadData函数-这按预期工作。

I now have another named function 我现在有另一个命名函数

SN.CreateJsonFromDate = function (settings) {

 .... 

  var SuccessLoad = function () {

        _dateLoader.hide();
        _wrapper.slideUp();



    }
}

Is it possible to use LoadData from SN.Reload Inside the SuccessLoad function in SN.CreateJsonFromDate ? 是否可以在SN.CreateJsonFromDate的SuccessLoad函数内部使用来自SN.Reload的LoadData?

The LoadData function call UpdateData on success an updates the HTML from the json data and I want to call this function again in SN.CreateJsonFromDate as this will generate a new json file. LoadData函数成功调用UpdateData会从json数据更新HTML,我想在SN.CreateJsonFromDate中再次调用此函数,因为这将生成一个新的json文件。

No, because simply LoadData does not exist outside the scope of SN.Reload 不,因为只是LoadDataSN.Reload范围之外不存在。

If you do want to re-use the LoadData function, do not restrict it's scope to being inside SN.Reload and instead, perhaps (depending on what you want), attach it to the namespace as SN.LoadData 如果您确实想重用LoadData函数,请不要将其范围限制在SN.Reload ,而是(取决于您的需要)将其作为SN.LoadData附加到名称空间。

Yes, if you instantiate SN.Reload . 是的,如果您实例化SN.Reload for example: 例如:

var obj = new SN.Reload(settings);

then you can use LoadData from this object, like this: 那么您可以从该对象使用LoadData ,如下所示:

obj.LoadData();

And yes you have to make LoadData public using this: 是的,您必须使用以下方法将LoadData公开:

this.LoadData = function(){/*your code*/}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM