How can I pass a parameter to a setTimeout() callback?

前端 未结 28 1899
既然无缘
既然无缘 2020-11-21 07:31

I have some JavaScript code that looks like:

function statechangedPostQuestion()
{
  //alert("statechangedPostQuestion");
  if (xmlhttp.readyState==         


        
28条回答
  •  无人共我
    2020-11-21 08:04

    @Jiri Vetyska thanks for the post, but there is something wrong in your example. I needed to pass the target which is hovered out (this) to a timed out function and I tried your approach. Tested in IE9 - does not work. I also made some research and it appears that as pointed here the third parameter is the script language being used. No mention about additional parameters.

    So, I followed @meder's answer and solved my issue with this code:

    $('.targetItemClass').hover(ItemHoverIn, ItemHoverOut);
    
    function ItemHoverIn() {
     //some code here
    }
    
    function ItemHoverOut() {
        var THIS = this;
        setTimeout(
            function () { ItemHoverOut_timeout(THIS); },
            100
        );
    }
    function ItemHoverOut_timeout(target) {
        //do something with target which is hovered out
    }
    

    Hope, this is usefull for someone else.

提交回复
热议问题