I have a method that performs an asynchronous ajax request to get some permission information from the server (SharePoint) to beeing cached via JS so that I can access it for CSR. Sometimes this takes a little longer (500ms)... not always, but sometimes the rest of the script is loaded so fast, that it needs the permission before the ajax request ist completed entirely.
Is there a simple method to just delay the output of the method? I tried different stuff with SetTimeout() but notthing worked out.
Here is the simplyfied code...
var Permissions = {
get: function(key) {
while (this.cacheLoaded != true) {
wait(100); //<-- This is what I need
}
return (typeof this[key] != 'undefined' && this[key] == true) ? true : false;
},
cacheLoaded: false,
buildCache: function () {
// load permissions asynchronously by ajax
.
.
.
Permissions.cacheLoaded = true;
}
}
Permissions.buildCache();
.
.
.
doLotsOfOtherStuff();
.
.
.
//Here is the actual call of the get-function:
if (Permission.get('ALOW_DELETE')) {
deleteSomething();
}
Of course I could switch the Ajax requst to be synchronous. But this would delay the whole script loading. So actually I want to delay jsut the getter for a couple of milliseconds until the request is finished.