Home  >  Article  >  Web Front-end  >  IE7 browser window size change event executes multiple times bug and resize problem under IE6/IE7/IE8_jquery

IE7 browser window size change event executes multiple times bug and resize problem under IE6/IE7/IE8_jquery

WBOY
WBOYOriginal
2016-05-16 15:43:511171browse

This article mainly uses code examples to introduce to you the bug of IE7 browser window size change event executing multiple times and the resize problem under IE6/IE7/IE8; step by step introduction, first introduce to you the IE7 browser window size change event executing multiple times bug, please see below for specific problem analysis and solutions.

var resizeTimer = null;
$(window).resize(function() {
 if (resizeTimer) clearTimeout(resizeTimer);
 resizeTimer = setTimeout("alert('mm')", 500);
});

There is also a solution by judging the parity of the variable (I think this method is okay)

The code is as follows:

var n=0;
$(window).resize(function(){
 if(n%2==0){
  alert("mm");
 }
 n++;
});

Whether it is encapsulated by jquery or native js, this bug will occur

Solution to resize event executing multiple times under JQuery in IE6/IE7/IE8

When using jQuery's resize event, I found that the resize time will be executed twice every time the browser window size is changed. I searched on Baidu and found a solution,

Use setTimeout to solve this problem. The code is as follows:

var resizeTimer = null;
$(window).resize(function() {
 if (resizeTimer) clearTimeout(resizeTimer);
 resizeTimer = setTimeout("alert('mm')", 500);
});

There is also a solution by judging the parity of variables (I think this method is okay), the code is as follows:

var n=0;
$(window).resize(function(){
 if(n%2==0){
  alert("mm");
 }
 n++;
});

The above is the article’s focus on the multiple execution bug of the IE7 browser window size change event and the resize problem under IE6/IE7/IE8. I hope it will be helpful to everyone.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn