Reputation: 203
Here is Javascript code:
if (selected && this.env.contentframe && !list.multi_selecting)
this.preview_timer = setTimeout(function() {
ref.msglist_get_preview();
alert('done');
}, list.dblclick_time);
else if (this.env.contentframe)
this.show_contentframe(false);
Here is msglist_get_preview()
function code:
this.msglist_get_preview = function()
{
var uid = this.get_single_uid();
if (uid && this.env.contentframe && !this.drag_active)
this.show_message(uid, false, true);
};
And below id the show_message()
function:
this.show_message = function(id, safe, preview)
{
if (!id)
return;
var win, target = window,
url = this.params_from_uid(id, {_caps: this.browser_capabilities()});
if (preview && (win = this.get_frame_window(this.env.contentframe))) {
target = win;
url._framed = 1;
}
url = this.url(preview ? 'preview': 'show', url);
this.location_href(url, target, true);
}:
What I want to alert when a function ref.msglist_get_preview();
process is complete. I have tried but every time alert appears first then function loads.
How can I achieve this ?
Thanks in advance.
Upvotes: 0
Views: 2855
Reputation: 35
setTimeout(function(){...}, 0) simply queues the code to run once the current call stack is finished executing. So the issue you are facing is because your >ref.msglist_get_preview(); is also doing asynchronous task and that's why it gets queued after the setTimeout and hence you get alert('done'); first and then your method execution. Following is the example to explain the same:
this.preview_timer = setTimeout(function() {
abc();
alert('done');
}, 500);
function abc(){
setTimeout(function(){
alert('abc');
},100)
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Here alert('abc') will be executed after alert('done')
For your problem you can use the javascript promises. Following code should solve for your problem:
if (selected && this.env.contentframe && !list.multi_selecting)
this.preview_timer = setTimeout(function() {
ref.msglist_get_preview().then(function(){
alert('done');
})
}, list.dblclick_time);
else if (this.env.contentframe)
this.show_contentframe(false);
function msglist_get_preview() {
return new Promise(function(resolve, reject) {
setTimeout((function() {
alert('msglist_get_preview worked');
resolve("Stuff worked!");
}), 1000);
});
}
Upvotes: 1