1

I need to run following code

document.getElementById("someID").focus();

after below code

setTimeout(function(){loadEditor(param);}, 50);

But i am unable to make it asynchronous.Since I can not modify

function loadEditor(param){ /* some stuff */  }

Is there anyway out to do this? What I have tried till now

setTimeout(function(){
    loadEditor(param);
    document.getElementById("someID").focus();
}, 50);

and

setTimeout(function(){
    $.when( loadEditor('question_stem-text') ).done(function() {
        document.getElementById("someID").focus();
        });
}, 50);

Not successful

5
  • 2
    What does loadEditor do? Commented Jun 14, 2017 at 5:56
  • It is initializing lots of values and loading editor for fields Commented Jun 14, 2017 at 5:57
  • In your first attempt. Have you tried console.log() to check if it gets called? Commented Jun 14, 2017 at 6:05
  • yes its getting called Commented Jun 14, 2017 at 6:05
  • Check if loadEditor implements a callback call after it's done (I believe it's async, so it should in theory). Otherwise you should edit loadEditor and add a callback call when it's done. Commented Jun 14, 2017 at 6:15

1 Answer 1

2
//ES6++
const delay = new Promise( (resolve, reject) => {
    setTimeout( () = > {
        loadEditor(param);
        resolve();
    }, 50);
});

delay.then( () => {
    document.getElementById("someID").focus();
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.