Google에이 작업을 시도했지만 해결책을 찾지 못했습니다. 그래서 문제는 사용자 창 너비가 특정 너비로 변경된 경우 한 번 실행하려고하는 것입니다. 어떤 이유로 변수가 이고 정의되지 않은이 함수 내에 있습니다. 사용자가 900 이상 윈도우 폭이있는 경우 변수가 함수로 전달되지 않습니까?
jQuery(document).ready(function ($) {
var windowWidth = $(window).width();
var n = 1;
if(windowWidth >= 900) {
var tc = 3;
$(".debug").html(tc); // Making sure if user window width is 900 or more and tc is 3 on page load.
} else if (windowWidth <= 900) {
var tc = 2;
$(".debug").html(tc); // Making sure if user window width is 900 or less and tc is 2 on page load.
}
function liveWidthChange(){
var windowWidth = $(window).width();
$(".debug2").html("<b>current width:</b> " + windowWidth + " - <b>tc:</b> " + tc);
if (tc == 3 && windowWidth <= 900) {
// Shows how many times executed if activly changing browser width and the current value of tc
$(".debug").html("tc Value: " + tc);
var tc = 2;
}
}
// If the browser changes size
var RS = false;
$(window).resize(function() {
if (RS !== false) clearTimeout(RS);
RS = setTimeout(liveWidthChange, 200);
});
});
그래서, 그것은 사용자가 브라우저의 크기를 조정하고는 900 아래에 간다 경우, 뭔가를 실행 tc
변수에 3을 설정
여기에 코드입니다.
미리 감사드립니다.
var windowWidth = $(window).width();
var n = 1;
var tc; // here, at the top.
그런 다음 당신은 당신이 tc
의 값을 설정 한 각 위치에 var
을 제거해야합니다 : 모든 기능이에 액세스 할 경우
[closure] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Closures)라고하며 [이 기사의 내용] (http : //blog.jcoglan .com/2012/01/19/the-cost-of-privacy /)는 도움이 될 수 있습니다 –