JS - Keys

by r_costa

HTML

<div id="text"></div>
<div id="div1"></div>

JavaScript

var d0;
var d1;

var subtotal = 0;
var total = 0;

var div1 = document.getElementById("div1");
var text = document.getElementById("text");

window.addEventListener("keydown", dealWithKeyDown, false);
window.addEventListener("keyup", dealWithKeyUp, false);

function dealWithKeyDown(event) {
    if (event.keyCode == 40) {
    
        if (typeof d0 === 'undefined') {
            d0 = new Date();
        }
        
        d1 = new Date();        
        subtotal = Math.round((d1.getTime() - d0.getTime()) / 1000);  
              
        div1.innerHTML = subtotal;
        text.innerHTML = "Text to show";
    }
}

function dealWithKeyUp(event) {
    if (event.keyCode == 40) {
    	total = total + subtotal;
        text.innerHTML = "";
        d0 = undefined;   
        console.log(total);
       // Qualtrics.SurveyEngine.setEmbeddedData("readingtime", total);
    }
}