Morten Hagh
Morten Hagh

Reputation: 2113

Calling named function from other named function in javascript

I have run into a problem with a named function in 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.

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 ?

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.

Upvotes: 1

Views: 141

Answers (2)

Sachin
Sachin

Reputation: 1218

Yes, if you instantiate SN.Reload. for example:

var obj = new SN.Reload(settings);

then you can use LoadData from this object, like this:

obj.LoadData();

And yes you have to make LoadData public using this:

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

Upvotes: 2

Adam Jenkins
Adam Jenkins

Reputation: 55613

No, because simply LoadData does not exist outside the scope of SN.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

Upvotes: 2

Related Questions