Himmators
Himmators

Reputation: 15006

Timeout doesn't time out

I have a set of functions in a javascript object. Here is one of them:

addImage: function(evt) {
        var file = evt.target.files[0];
        /* Only process image files.*/
        if(file.type.match('image.*')) {
            /*Add image to FileReader object*/
            var reader = new FileReader();
            reader.readAsDataURL(file);
            /*Add image to local storage */
            reader.onload = (function(theFile) {
                return function(e) {
                    localStorage.setItem(underlayer.url, e.target.result);
                    //add image
                    underlayer.setImage();
                    underlayer.setPosition();
                    setTimeout(underlayer.setHeight(), 5000);

                };
            })(file);
        }
    },

I want underlayer.setHeight() to trigger after a delay it's necessary because it reads the height of an image that is loaded into the local storage, and that takes a couple of milliseconds.However, underlayer.setHeight is triggered emidiately, not with a delay.

Upvotes: 1

Views: 108

Answers (2)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324620

By putting () after setHeight, you are CALLING the function, and passing its RETURN VALUE to setTimeout. This is clearly not what you're trying to do.

Remove the () and it should work fine.

EDIT: Forgot about context. setTimeout(function() {underlayer.setHeight();},5000); is probably your best bet, then.

Upvotes: 8

Praveen
Praveen

Reputation: 56501

You need to change your setTimeout code.

setTimeout(function(){underlayer.setHeight()},5000);

Upvotes: 1

Related Questions