javascript快捷键/停止间隔功能

时间:2016-03-08 19:56:20

标签: javascript

我为javascript函数创建了一个快捷键。我设置S键开始这个,所以我设置Z键清除间隔功能,但我m tired about this and when press Z key the Interval doesn停止:(

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);
}

    }
}

帮我,我想按Z键和间隔停止,但我不能......

更新:此代码正常运行。使用S键开始,使用Z键停止功能。

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;
    }
}

2 个答案:

答案 0 :(得分:1)

你似乎没有听一个事件来听按键。

jQuery的:

<uib-tabset type="tabs">
  <uib-tab heading="Event Workflow Activities">
    <div ng-include src="'webapp/event/EventWorkflowActivities.tpl.html'"></div>        
  </uib-tab>
</uib-tabset>

Vanilla JS:

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

答案 1 :(得分:0)

您还必须以避免多次启动计时器的方式对其进行编码。你应该更像这样构造它(移动函数外的定时器的var):

  IF  @lsUseLikeForCodeWhereCondition = 'N'
    BEGIN
        select * from mytable where idn_orn_i = @code 
    END
  ELSE 
    BEGIN
        select * from mytable where idn_orn_i Like  @code + '%'
    END