<div class="planeta" id="planeta">
<img src="planeta.jpg" style="width: 150px; height: 150px;">
<div class="text-blocka">
<h4>Kepler-186f</h4>
<p>550 light years away</p>
</div>
</div>
代码的第一部分工作,图像开始闪烁,但我希望它在释放密钥时取消闪存,而这部分对我不起作用。下面是我到目前为止所写的javascript。提前谢谢!
function checkKeydowna(akey) {
if (akey.keyCode == "65") {
var img = document.getElementById('planeta');
var interval = window.setInterval(function(){
if(img.style.visibility == 'hidden'){
img.style.visibility = 'visible';
} else {
img.style.visibility == 'hidden'
}
}, 50);
}
}
window.addEventListener("keyup", checkKeyupa, false);
function checkKeyupa(abkey) {
if (abkey.keyCode == "65") {
delete window.setInterval();
}
}
答案 0 :(得分:1)
addEventListener
checkKeydowna
。{/ li>
delete window.setInterval();
毫无意义。没有清除间隔。你应该用window.clearInterval(intervalInstance)
清除间隔。因此,如果要清除间隔,则应在设置时将其存储在全局变量中。这就是为什么我声明var interval = false;
这也意味着间隔没有设置。
var interval = false;
function checkKeydowna(akey) {
if (akey.keyCode == "65") {
var img = document.getElementById('planeta');
if (!interval) {
interval = window.setInterval(function() {
if (img.style.visibility == 'hidden') {
img.style.visibility = 'visible';
} else {
img.style.visibility = 'hidden'
}
}, 50);
}
}
}
document.body.addEventListener("keyup", checkKeyupa, false);
document.body.addEventListener("keydown", checkKeydowna, false);
function checkKeyupa(abkey) {
if (abkey.keyCode == "65") {
window.clearInterval(interval);
interval = false;
var img = document.getElementById('planeta');
img.style.visibility = 'visible';
}
}
&#13;
<img style='visibility:visible' src="https://www.google.com.tr/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png" id="planeta" />
&#13;
答案 1 :(得分:0)
你需要在CheckTimeout或setInterval中放置checkKeyupa。
window.addEventListener("keyup", ()=>setTimeout(checkKeyupa, 50), false);
因为你需要确保checkKeyupa进入事件队列后面的间隔,并在间隔get执行后执行。