简体   繁体   English

实施并按住按钮

[英]Implement press and hold on a button

Thank you for the previous (deleted by moderator) explanation and the simple implementation but it does not work out for me. 感谢您之前(由主持人删除)的解释和简单的实现,但对我而言不起作用。 The 'holdit' function works but it is not is not steady probably because there is an 'onmouseup' in the 'holdit' function too, Even if I disable the onmouseup at the HTML button it's not very steady. 'holdit'函数可以工作,但是它不是很稳定,可能是因为'holdit'函数中也有一个'onmouseup'。即使我在HTML按钮上禁用了onmouseup,它也不是很稳定。 Maybe it's better to use an addEventListener- onmousedown-interval function but again I don't know how to implement it in the simplest possible way. 也许最好使用addEventListener- onmousedown-interval函数,但是我也不知道如何以最简单的方式实现它。 Here's the complete function that shows a pressed button and increases the timeSeconds var by one. 这是完整的功能,它显示一个按下的按钮,并将timeSeconds var加1。 For safety the var. 为了安全起见, number is within a limit. 数量在限制范围内。 Please help. 请帮忙。

     HTML:
    <img id="but4" class="button" src= "//:0" onmousedown="timesecPlus();"onmouseup="timesecPlsUp();"/>

     JAVASCRIPT:
    function timesecPlus() {
    var pmknop = document.getElementById('but5');
    pmknop.src = secminBtndwn; //inline Base64 data: button image down (pressed) 

    timeSeconds = ((timeSeconds>wedstrijdperiode.seconden-6)?(timeSeconds):(++timeSeconds)); //You can ++ chase-back the timeseconds until 5 sec's from   period start-time
    displayTime( timeSeconds ); 
    };

    function timesecPlsUp() {
    var pmknop = document.getElementById('but5'); 
    pmknop.src = secminBtn; //inline Base64 data: button image up (normal)
    };

   // Things I tried:
   //holdit(pmknop, function () { ++timeSeconds ; displayTime( timeSeconds );}, 2000, 2);
   //pmknop = pmknop.addEventListener('mousedown', function() { interval = setInterval(timesecPlus (), 2000); });


    function holdit(btn, action, start, speedup) {
    var t;

    var repeat = function () {
    action();
    t = setTimeout(repeat, start);
    start = start / speedup;
    }
    btn.onmousedown = function() {
    repeat();
    }
    btn.onmouseup = function () {
    clearTimeout(t);
    }
     }; 

Simple implementation: 简单实施:

var btn = document.getElementsByClassName('button')[0];
holdit(btn, function () { timeSeconds++ ; displayTime( timeSeconds );}, 1000, 2);

Implementation without holdit: 没有holdit的实现:

var btn = document.getElementsByClassName('button')[0];
var couterFunc, couter=0;
btn.addEventListener('mousedown',function(){couterFunc = setInterval(update,1000); update()})
btn.addEventListener('mouseup',function(){clearInterval(couterFunc)})

/* function that will fire when button press*/
function update(){console.log(++couter)};

The holdit function is taking four variables. holdit函数采用四个变量。 The first: btn, is the button id. 第一个:btn,是按钮的ID。 This is used to determine the action performed whenever the mouse is clicked. 这用于确定每当单击鼠标时执行的操作。

The second variable is a reference to a function. 第二个变量是对函数的引用。 Its called a callback function, since you will be passing a function that will be Caaalleed whenever you call holdit. 它称为回调函数,因为您将在每次调用holdit时传递一个将被Caaalleed的函数。

The last two variables simply determine when and how long to delay the execution of the repetition and and by how much each repetition will speed up by. 最后两个变量仅确定延迟重复执行的时间和时间,以及每次重复执行将加速多少时间。

var repeat = function () {
   action();
   t = setTimeout(repeat, start);
   start = start / speedup;
}

Repeat is a recursive function that will be called after 'start' number of milliseconds and be repeated more frequently after each iteration. Repeat是一个递归函数,将在“开始”毫秒后调用,并在每次迭代后更频繁地重复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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