mc805
mc805

Reputation: 137

JSONP, are there side effects?

The code below works the way I want it to, but if I look in the developer console, I can see, every time it runs it appends a new script (which I know seems obvious by the last line appendChild).

However, My question is, will this affect anything negatively, such as performance, in the end I'm thinking of having this run at an interval of maybe 30 seconds or less. That would definitely start adding a massive amount of scripts to document.body?

If this will create side affects, how do should I go about fixing it?

No JQUERY... also I'm trying to keep it simple.

window.myJsonpCallback = function(data) {
        AppDispatcher.handleNewAction({
            actionType:AppConstants.PULL_DATA,
            item: data
        })
};

var scriptEl = document.createElement('script');
scriptEl.setAttribute('src',
    'http://api.xxxx.us/api/stops/near/37.0641593/-76.4929986/?callback=myJsonpCallback');
document.body.appendChild(scriptEl);

'

Will there be any side effects, if I leave as is?

Upvotes: 0

Views: 39

Answers (1)

Olavi Sau
Olavi Sau

Reputation: 1649

You can delete the old script every time you load it.

body.removeChild(document.getElementById("jsonp"))
var scriptEl = document.createElement('script');
scriptEl.id = "jsonp";
scriptEl.setAttribute('src',
'http://api.xxxx.us/api/stops/near/37.0641593/-76.4929986/?callback=myJsonpCallback');
document.body.appendChild(scriptEl);

Upvotes: 1

Related Questions