简体   繁体   中英

javascript shortcut key / stop Interval function

i make a shortcut key for javascript function . i set the S key for start this and i so set the Z key for clear Interval function but i m tired about this and when press Z key the Interval doesn t stop :(

var isCtrl = false;
document.onkeydown=function(e){
    if(e.which == 83) {




var elem = document.elementFromPoint( cursorX,cursorY );
elem.addEventListener('click', function() {
    console.log('clicked')
}, false);

var support = true;

try {
    if (new MouseEvent('click', {bubbles: false}).bubbles !== false) {
        support = false;
    } else if (new MouseEvent('click', {bubbles: true}).bubbles !== true) {
        support = false;
    }
} catch (e) {
    support = false;
}

var refreshIntervalId = setInterval(function() {
    if (support) {
        var event = new MouseEvent('click');
    }else{
        var event = document.createEvent('Event');
        event.initEvent('click', true, true);
    }
    elem.dispatchEvent(event);
},10);
var cursorX;
var cursorY;
cursorX = 0; cursorY = 0;
document.onmousemove = function(e){
    cursorX = e.clientX;
   cursorY = e.clientY;
   elem = document.elementFromPoint(e.clientX, e.clientY);
}


if(e.which == 90) {
clearInterval(refreshIntervalId);
}

    }
}

help me i want to press Z key and Interval stop but i can`t ...

UPDATE : this code work correctly . use S key for start and Z key for stop the function .

var elem = document.elementFromPoint( cursorX,cursorY );
elem.addEventListener('click', function() {
    console.log('clicked')
}, false);

var support = true;

try {
    if (new MouseEvent('click', {bubbles: false}).bubbles !== false) {
        support = false;
    } else if (new MouseEvent('click', {bubbles: true}).bubbles !== true) {
        support = false;
    }
} catch (e) {
    support = false;
}


var cursorX;
var cursorY;
cursorX = 0; cursorY = 0;
document.onmousemove = function(e){
    cursorX = e.clientX;
   cursorY = e.clientY;
   elem = document.elementFromPoint(e.clientX, e.clientY);
}



var refreshIntervalId;
window.addEventListener("onkeydown", keyDown,true);
window.addEventListener("keydown", keyDown);

function keyDown() {
    var e = window.event;
    switch (e.keyCode) {
        case 83:
            start();
            break;
        case 90:
            stop();
            break;
    }
}

function start() {
    stop();
    refreshIntervalId = setInterval(function() {
    if (support) {
        var event = new MouseEvent('click');
    }else{
        var event = document.createEvent('Event');
        event.initEvent('click', true, true);
    }
    elem.dispatchEvent(event);
},1000);
}

function stop() {
    if (refreshIntervalId != null) {
        clearInterval(refreshIntervalId);
        refreshIntervalId = null;
    }
}

You doesn't seem to make an event to listen to keypress.

jQuery:

$(window).keypress(function(e) {
    if (e.which == 90) {
        clearInterval(refreshIntervalId);
    }
});

Vanilla JS:

var keyEvent = function (e) {
    if (e.which == 90) {
        clearInterval(refreshIntervalId);
    }
};
window.addEventListener('keypress', keyEvent);

You also have to code it in a way to avoid starting the timer more than once. You should construct it more like this (move the var for the timer outside the function):

var refreshIntervalId;
window.addEventListener("onkeydown", keyDown,true);
window.addEventListener("keydown", keyDown);

function keyDown() {
    var e = window.event;
    switch (e.keyCode) {
        case 83:
            start();
            break;
        case 90:
            stop();
            break;
    }
}

function start() {
    stop();
    refreshIntervalId = setInterval(function() {
        // code...
    },10);
}

function stop() {
    if (refreshIntervalId != null) {
        clearInterval(refreshIntervalId);
        refreshIntervalId = null;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM