我有一个函数,我将其称为带有其他任务/步骤的幻灯片。间歇性地,函数内的滑动代码被调用两次。有人能发现我做错了吗?
全局变量
var currIndex = 0;
带问题的功能
function PreNext(direction) {
alert('Test #1');
var thisMaxlen = homes.length - 1; // homes is an array.
var ctrl_toolTip = $('#controlSlideShow .tooltip');
$(ctrl_toolTip).slideUp('slow' function () {
alert('Test #2');
if (direction == 'Next') {
(currIndex >= thisMaxlen ? currIndex = 0 : currIndex++);
}
else {
(currIndex <= 0 ? currIndex = thisMaxlen : currIndex--);
}
});
alert('Test #3');
};
间歇性地,滑动被调用两次 结果
Test #1
Test #2
Test #2
Test #3
答案 0 :(得分:1)
我想到的唯一原因是你在ctrl_toolTip
确保$('#controlSlideShow .tooltip');
仅返回单个元素。
答案 1 :(得分:1)
确定。我无法弄清楚为什么双程正在发生。但是我能够采用一种解决方法来适应双重传递。
function PreNext(direction) {
alert('Test #1');
var dblPassFix = 0; // To fix the double pass in the slideup
var thisMaxlen = homes.length - 1; // homes is an array.
var ctrl_toolTip = $('#controlSlideShow .tooltip');
$(ctrl_toolTip).slideUp('slow' function () {
alert('Test #2');
dblPassFix++;
if (direction == 'Next') {
(currIndex >= thisMaxlen ? currIndex = 0 : currIndex++);
if (dblPassFix > 1) {
currIndex--;
}
...
}
else {
(currIndex <= 0 ? currIndex = thisMaxlen : currIndex--);
if (dblPassFix > 1) {
currIndex++;
}
...
}
});
alert('Test #3');
};