简体   繁体   中英

JQuery/Javascript invalid argument script error on window resize in IE8

I'm getting a script error in IE8 when using the following snippet for reacting on window resize event, although the browser executes the function 'dummyFunc()' as intended:

var jq = jQuery.noConflict();
var resizeTimer;
  jq(window).resize(function() {
      clearTimeout(resizeTimer);
      //resizeTimer = setTimeout(reloadSlider(), 100, 'JavaScript');
      resizeTimer = setTimeout(dummyFunc(), 100, 'JavaScript');
  });

function dummyFunc() {
    alert('hurra');
}

So, the problem is not that the browser does not alert, the problem is rather that there is a "script error" message displayed in Internet Explorer, which is unlovely. I'd appreciate any hints on how to get rid of the error message.

Thank you

Change dummyFunc() to dummyFunc . As you have it, you are executing the function immediately, and then passing undefined (the result of the function call) to setTimeout .

You want to instead pass a reference to the function:

resizeTimer = setTimeout(dummyFunc, 100, 'JavaScript');

You'll have to run ie8 while debugging the script. Press F12 while viewing the page and start debugging from there. Then you'll have to analyze the point where you get the script error.

resizeTimer is undefined until the first setTimeout. Try if(resizeTimer) { clearTimeout(resizeTimer); ... if(resizeTimer) { clearTimeout(resizeTimer); ...

You are executing dummyFunc immediately by calling it. dummyFunc() gets replaced by the return value of the function, which is undefined.

resizeTimer = setTimeout(dummyFunc(), 100, 'JavaScript');

Just omit the bracers at dummyFunc to pass it as a reference.

resizeTimer = setTimeout(dummyFunc, 100);

I've never seen the last parameter you passed, so I left it out. This could very easily be throwing errors in IE8.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM