javascript - get wrong value when window resize -
first, window width , minus assign variable,
second, resize window , want value current window width minus something
but wrong value, alert 2 value 1 right , wrong,i not know why , how fix it, me, thx
my html code
<p class="test">click me value</p>
js
(function(){ test(); $(window).resize(function(){ test(); }); }()); function test() { var t = $(window).width()-74; alert('one ' +t); $(document).on('click', '.test',{t: t}, get); } function get(event) { var l = event.data.t alert('two ' +l) }
the fiddle version http://jsfiddle.net/dxcqcv/xetbhwpv/1/
i not sure values expecting get, take @ resize
function documentation on jquery api:
code in resize handler should never rely on number of times handler called. depending on implementation, resize events can sent continuously resizing in progress (the typical behavior in internet explorer , webkit-based browsers such safari , chrome), or once @ end of resize operation (the typical behavior in other browsers such opera).
as can see, browsers call resize @ end of resizing, other keep calling function on process of resizing. means different values call when not resizing , while in resizing operation.
i made changes code in order become cleaner. please take @ jsfiddle using developer console (press f12 on browsers):
http://jsfiddle.net/xetbhwpv/7/
$(document).ready(function(){ test(); $(window).resize(function(){ test(); }); }); function test() { var t = $(window).width() - 74; console.log('one ' + t); $('.test').off('click'); $('.test').on('click', {t: t}, getinfo); } function getinfo(event) { var l = event.data.t console.log('two ' +l) }
as can see now, last two xx
messages equal last resize
messages.
Comments
Post a Comment