事件轮询实时

时间:2013-10-16 14:44:41

标签: javascript events event-handling

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函数调用中最终变量。请帮忙。

1 个答案:

答案 0 :(得分:2)

在每个处理函数的末尾调用getResult():

console.log(getResult());