ObiHill
ObiHill

Reputation: 11896

Delaying javascript code execution in called function

So I have this class that has methods that capture some specific information. One very important method is getViewportWidth.

Due to a bug/feature in Android 2.2 or so, the widths that are reported are not quite accurate for users using this device platform (You can see more about this here: How do I find the virtual viewport/screen width using Javascript?

The workaround seems to be to delay script execution of the whole script. However, because I need to take care of this transparent from the user, I want to instead delay script execution at the function level i.e. at my getViewportWidth function.

So that a user can just call:

MyClass.getViewportWidth();

And the delay would have been taken care of.

So basically, I'm trying to find a way to delay code execution in my getViewportWidth function by 200 milliseconds to accomodate this bug/feature. How can I do this?!

Upvotes: 0

Views: 535

Answers (1)

Halcyon
Halcyon

Reputation: 57721

You can use setTimeout to delay the execution of a function by some milliseconds. Use like:

var text = "Hi!";
setTimeout(function () {
    alert(text);
}, 200); // 200 ms

Upvotes: 2

Related Questions