【问题标题】:Making a button which on press or click executive a function until unpress the buttons制作一个按下或单击执行功能的按钮,直到松开按钮
【发布时间】:2021-11-17 13:04:11
【问题描述】:

这是我试图使用 HTML 按钮使对象移动但在单击它时执行的功能我希望它在按下按钮时连续执行功能 下面是 JavaScript 中按钮的代码

var increment = 1; 

function moveUp()
{
camera.position.z +=increment;
}
function moveDown()
{
camera.position.z -= increment;
}
function moveRight()
{
camera.position.x +=increment;
}
function moveLeft()
{
camera.position.x -= increment;
}

【问题讨论】:

  • 使用setInterval,创建一个计时器,存储返回值/id,当你再次点击/按下按钮时,用户clearInterval清除/终止执行。

标签: javascript html three.js


【解决方案1】:

你应该使用事件。

  1. 检测 keyDown 事件。设置一个变量 'buttonUpIsDown'=true 并调用 - 或设置一个计时器以每 10 毫秒调用一次 - 一个函数。
  2. While/If (buttonUpIsDown) { 增加内容};
  3. 检测 keyUp 事件并将变量设置为 false。

https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-16
    • 2016-12-05
    • 2022-07-16
    • 1970-01-01
    • 1970-01-01
    • 2012-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多