最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - settimeout issue in IE8 - Stack Overflow

programmeradmin4浏览0评论

I am facing a strange issue while using javascript setTimeout function in IE8. I want to use the 'setTimeout' function like this -

setTimeout(timeout,2000, {name:'saarthak'});

    function timeout(opts)
    {       
        alert('hello ' + opts.name);
    }

the third parameter of the setTimeout is the argument that I want to pass to the calling function. This is working perfect fine in FF, Chrome but not in IE8.

Does anybody have any clue what could be done? Or any work around of achieving this?

Thanks

I am facing a strange issue while using javascript setTimeout function in IE8. I want to use the 'setTimeout' function like this -

setTimeout(timeout,2000, {name:'saarthak'});

    function timeout(opts)
    {       
        alert('hello ' + opts.name);
    }

the third parameter of the setTimeout is the argument that I want to pass to the calling function. This is working perfect fine in FF, Chrome but not in IE8.

Does anybody have any clue what could be done? Or any work around of achieving this?

Thanks

Share Improve this question asked Aug 10, 2011 at 7:38 saarthaksaarthak 1,0142 gold badges12 silver badges27 bronze badges 1
  • 1 You could've been finding it yourself: Note that passing additional parameters to the function in the first syntax does not work in Internet Explorer. @developer.mozilla/en/window.setTimeout – KooiInc Commented Aug 10, 2011 at 8:15
Add a ment  | 

2 Answers 2

Reset to default 11

Probably not supported there, so have this instead:

window.setTimeout(function() {
    timeout({name:'saarthak'});
},2000);

Meaning call your function from within anonymous function.

If you want to call timeout with changing variable (e.g. calling timeout in loop with lot of names) you can use also in IE8:

var names = ["saarthak", "saarthak2", "saarthak3"]; 
for (var q in names) {
  setTimeout(
    (function(opts){
          return function(){
                    alert ("hello " + opts.name)            
                  }
     })({name:names[q]}), 2000);
}

see: http://jsfiddle/q4HYz/

发布评论

评论列表(0)

  1. 暂无评论