Click here to Skip to main content
15,887,746 members
Home / Discussions / JavaScript
   

JavaScript

 
GeneralRe: Changing a video at a certain time Pin
Citizen Vik4-Apr-17 7:47
Citizen Vik4-Apr-17 7:47 
GeneralRe: Changing a video at a certain time Pin
ZurdoDev4-Apr-17 7:49
professionalZurdoDev4-Apr-17 7:49 
GeneralRe: Changing a video at a certain time Pin
Citizen Vik4-Apr-17 9:34
Citizen Vik4-Apr-17 9:34 
QuestionHow I can add methods to add or get values in a dictionary object ? Pin
Izis30-Mar-17 0:57
Izis30-Mar-17 0:57 
QuestionNode js Reading HTML page Pin
Member 1304854330-Mar-17 0:31
Member 1304854330-Mar-17 0:31 
QuestionHow to implement a custom javascript type to act like a dictionary ? Pin
Izis29-Mar-17 10:54
Izis29-Mar-17 10:54 
QuestionIs this closure correct ? Pin
Izis29-Mar-17 7:27
Izis29-Mar-17 7:27 
AnswerRe: Is this closure correct ? Pin
Richard Deeming29-Mar-17 7:49
mveRichard Deeming29-Mar-17 7:49 
No. You're expecting the url parameter to be passed to the wrapper function, and you're not returning a function.

Try this instead:
JavaScript
var cachedAjax = (function(){
    var cache = {};
    return function(url){
        if (cache.hasOwnProperty(url)) {
            return cache[url];
        }
        
        var data = $.ajax({
            url: url,
            async: false
        }).responseText;
        
        cache[url] = data;
        return data;
    };
})();

However, you should avoid synchronous AJAX requests, as they will block the UI for the duration of the request. You should try to rewrite your code using callbacks or promises[^] instead.
JavaScript
var cachedAjax = (function(){
    var cache = {};
    return function(url){
        if (cache.hasOwnProperty(url)) {
            return cache[url];
        }
        
        var promise = $.ajax({ url: url });
        cache[url] = promise;
        return promise;
    };
})();

cachedAjax("someUrl").then(function(data){
    // Do something with the data here...
});




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: Is this closure correct ? Pin
Izis29-Mar-17 8:56
Izis29-Mar-17 8:56 
QuestionPrint HTML file using ActiveX Pin
Christopher Cote27-Mar-17 3:36
Christopher Cote27-Mar-17 3:36 
SuggestionRe: Print HTML file using ActiveX Pin
Richard Deeming27-Mar-17 7:49
mveRichard Deeming27-Mar-17 7:49 
QuestionDynamicaly Created Controls using Javascripted is not posting while submitting theform Pin
Member 1308703727-Mar-17 2:12
Member 1308703727-Mar-17 2:12 
AnswerRe: Dynamicaly Created Controls using Javascripted is not posting while submitting theform Pin
F-ES Sitecore28-Mar-17 22:41
professionalF-ES Sitecore28-Mar-17 22:41 
QuestionElements hidden - if visible do something Pin
Member 1307408621-Mar-17 3:42
Member 1307408621-Mar-17 3:42 
QuestionRe: Elements hidden - if visible do something Pin
ZurdoDev21-Mar-17 4:19
professionalZurdoDev21-Mar-17 4:19 
AnswerRe: Elements hidden - if visible do something Pin
Member 1307408621-Mar-17 4:34
Member 1307408621-Mar-17 4:34 
GeneralRe: Elements hidden - if visible do something Pin
ZurdoDev27-Mar-17 1:59
professionalZurdoDev27-Mar-17 1:59 
Questiondataset with java Pin
Member 1307317120-Mar-17 22:01
Member 1307317120-Mar-17 22:01 
AnswerRe: dataset with java Pin
Richard MacCutchan20-Mar-17 23:06
mveRichard MacCutchan20-Mar-17 23:06 
QuestionDisplay PDF in iframe from base64 string using blob url Pin
ankit1819-Mar-17 6:03
ankit1819-Mar-17 6:03 
SuggestionRe: Display PDF in iframe from base64 string using blob url Pin
ZurdoDev20-Mar-17 1:15
professionalZurdoDev20-Mar-17 1:15 
QuestionA more efficient approach? Pin
samflex10-Mar-17 3:54
samflex10-Mar-17 3:54 
AnswerRe: A more efficient approach? Pin
Nathan Minier16-Mar-17 4:41
professionalNathan Minier16-Mar-17 4:41 
GeneralRe: A more efficient approach? Pin
samflex17-Mar-17 3:01
samflex17-Mar-17 3:01 
AnswerRe: A more efficient approach? Pin
ZurdoDev16-Mar-17 5:38
professionalZurdoDev16-Mar-17 5:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.