0

Any idea why the function setTimeout() not working when I using in function

parent.parent(the function hide() fired but no delay).

setTimeout(parent.parent.ModalWindow.hide(), 5000);

Any idea what I am missing?

Michael
  • 11,410
  • 43
  • 120
  • 228
  • 1
    You're invoking the function, so naturally there's no delay. I assume the `.hide()` method relies on `ModalWindow` being the `this` value, so pass an anonymous function instead with that `parent.parent.ModalWindow.hide()` inside it. –  Aug 31 '15 at 21:11
  • Can you post example please?I am newer to JavaScript so many trivial things seems to me complicated. – Michael Aug 31 '15 at 21:19
  • @Oriol The posted duplicate doesn't appear to be a duplicate of this question at all. I can understand the confusion I guess, but it appears you've been hasty. We may have a "why isn't this code working" question, and not enough details -- it is close-able, sure, but not as a dupe of the selected question. Voting to reopen so it can be properly closed, unless the OP is able to improve it. – Chris Baker Aug 31 '15 at 21:43
  • 1
    @Michael The problem isn't `parent.parent`, the problem is the syntax you're using. When you pass a function reference like this to `setTimeout`, you don't include the parenthesis (`()`) -- you pass the function itself. So, `setTimeout(parent.parent.ModalWindow.hide, 5000);` -- note the lack of parenthesis after the function name. I am assuming that you have already checked `parent.parent` exists and all that, if not, you probably should, or consider refactoring -- calling nested methods like that can be a code smell. – Chris Baker Aug 31 '15 at 21:46
  • 1
    @ChrisBaker The same problem you explained (the parentheses run the function immediately) is explained in the dupe. Note `setTimeout(parent.parent.ModalWindow.hide)` may not work because the `this` value won't be set to `parent.parent.ModalWindow`. Therefore, as explained in the dupe, it's better to wrap the function inside an anonymous function expression. But maybe [Calling functions with setTimeout()](http://stackoverflow.com/q/3800512/1529630) was a better dupetarget, which explains `bind` can be used too. – Oriol Aug 31 '15 at 22:58

0 Answers0