settimeout issue in IE8

后端 未结 2 724
轻奢々
轻奢々 2021-01-20 15:58

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         


        
相关标签:
2条回答
  • 2021-01-20 16:19

    Probably not supported there, so have this instead:

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

    Meaning call your function from within anonymous function.

    0 讨论(0)
  • 2021-01-20 16:23

    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.net/q4HYz/

    0 讨论(0)
提交回复
热议问题