variables being emptied when brought into jquery
I am setting a variable on page load using
var lastVal;
var totHistory=0;
var positions = new Array();
function addHistory(obj){
totHistory++;
positions.push(obj.id);
}
when I try to access totHistory within jquery it returns to zero. I can confirm that totHistoy has a value using console.log outside of jquery function but it goes away within. How can I solve this?
$(function() {
lastVal = totHistory;
$("#slider").slider({
animate: true,
min: 1,
max: totHistory,
value:totHistory
slide: function(event, ui) {
if(lastVal>ui.value)
$(buildQ(lastV开发者_JAVA技巧al,ui.value)).hide('fast').find('.addComment').remove();
else if(lastVal<ui.value)
$(buildQ(lastVal,ui.value)).show('fast');
lastVal = ui.value;
}
});
});
My guess is that your
addHistory({id : "28"})
is called on body onload event. But the function passed to jQuery ($) is called on DOMReady which gets fired before onload. So, that function always sees the totHistory value as zero.
精彩评论