比较sessionStorage值时,Javascript函数无法调用
Javascript function not being able to call when sessionStorage value is compared
我目前正在创建一个flash库,每次按下新按钮查看新的flash时,我都希望看到页面重新加载以显示新的广告。我已经解决了console.log的问题,并确定sessionStorage变量设置正确,但当调用back/rand/next()函数时,它就会停止。任何提示都很有价值。
(编辑)它进入console.log("收到兰德按钮!");
代码
var flashcon, test, temp;
var back, rand, next;
back = document.getElementById('back');
rand = document.getElementById('rand');
next = document.getElementById('next');
flashcon = document.getElementById('flashcon');
function init() {
if (sessionStorage.getItem('ref') == 1) {
console.log('Back button ses received!');
back();
} else if (sessionStorage.getItem('ref') == 2) {
console.log('Rand button ses received!');
rand();
} else if (sessionStorage.getItem('ref') == 3) {
console.log('Next button ses received!');
next();
} else {
console.log('First time or a reset');
}
// Scripts for the buttons
back.onclick = function () {
console.log('Back button pressed');
sessionStorage.setItem('ref', 1);
location.reload();
};
rand.onclick = function () {
console.log('Rand button pressed');
sessionStorage.setItem('ref', 2);
location.reload();
};
next.onclick = function () {
console.log('Next button pressed');
sessionStorage.setItem('ref', 3);
location.reload();
};
}
// What switches the flashs
function back() {
console.log('Back function');
sessionStorage.clear();
if (c == 0) {
c = paths.length;
}
c--;
displayFiles();
download();
}
function rand() {
console.log('Rand function');
sessionStorage.clear();
if (c == paths.length - 1) {
c = -1;
}
c++;
displayFiles();
download();
}
function next() {
console.log('Next function');
sessionStorage.clear();
temp = c;
while (c == temp) {
c = Math.floor(Math.random() * paths.length);
}
displayFiles();
download();
}
您拥有凌驾于彼此名称之上的变量和函数。
back, next, rand
都是函数语句,它们被提升到对应变量名的... = document.getElementById('...')
之上。
请更改函数名称或元素对象名称,然后重试。
所以基本上转向:
var back, rand, next;
back = document.getElementById('back');
rand = document.getElementById('rand');
next = document.getElementById('next');
进入这个:
var $back, $rand, $next;
$back = document.getElementById('back');
$rand = document.getElementById('rand');
$next = document.getElementById('next');
当然,还要编辑代码中后面的匹配位置,使它们指向正确的变量。
相关文章:
- 如何从模块链中调用函数.导出到节点中
- 调用函数内部的函数
- 在javascript中调用c函数
- DropDownListFor赢得't在更改时调用函数
- Javascript页面调用函数
- 在动画结束之前调用函数
- 允许父窗口在其不同域的子iframe上调用函数
- 运行Infinite Scroll后调用函数时出现问题
- JavaScript:在调用函数的文本输入上按enter键
- 使用大括号和不使用bracs调用函数的区别
- javascript在事件上调用函数
- 从index.html调用函数,该函数无限循环
- 从带参数的字符串变量调用函数中的函数
- 为什么 JS 不在滚动时调用函数
- 是否可以在不更改上下文的情况下调用函数.apply
- 如何在ES6类中使用参数调用函数
- AngularJS:调用函数时编号不更新
- JavaScript中的立即调用函数表达式(IIFE)-传递jQuery
- 在JavaScript中调用函数时自定义此选项
- 调用函数中声明的变量