document.body.onclick = function(e) {
e = e || event
var target = e.target || e.srcElement
// variable target has your clicked element
innerId = target.id;
// do your stuff here.
isClicked = 1;
}
document.body.onmousemove = function()
{
hasMouseMoved = 1;
//console.log(hasMouseMoved);
}
window.onresize = function()
{
isresized =1;
//console.log(isresized);
}
window.onscroll = scroll;
function scroll()
{
alert("scroll event detected! " + window.pageXOffset + " " + window.pageYOffset);
isScrolled=1;
//console.log(isScrolled);
}
document.body.onkeypress = function(evt)
{
evt = evt || window.event;
if (evt.keyCode!=='undefined') {
// Do your stuff here
isPressed = 1;
// console.log(isPressed);
}
return isPressed;
}
function getResult() {
result = ***hasMouseMoved+"|"+isClicked +"|"+isPressed+"|"+isresized+"|"+isScrolled+***"|";
//console.log(result);
return result;
}
// return result;
//}
var final = getResult();
console.log(final);
所以我有这 5 个事件处理程序,它们操纵/处理用户与键盘和鼠标的交互。我有一个函数getResult
可以获取与这些函数相关的标志的变化。当我使用 HTML 页面运行此 Javascript 时,我需要getResults();
在输入/mousepressed/mousemoved/scrolled/resized 后不断地输入控制台以获取这些标志的更改。我希望这些更改能够实时显示,这些标志会随着交互而变化。
请帮助我了解如何轮询这些处理程序以便实时给出结果。到目前为止,标志 ( isPressed
/ isScrolled
/ isResized
/ hasMouseMoved
/ isClick
) 被赋值为 0,并且在getResults
使用 final 变量的函数调用中仍然如此。请帮忙。