简体   繁体   中英

How to click and hold down the button for a certain second with javascript?

I want to make a button that if a guest button number 1, then button number 2 will be clicked and then pressed down for about 3 seconds, is it possible using jQuery?

I already tried using the mousedown() and click() but it doesnt press it down, only click. and already tried to googled it but doesnt found the solution

EDIT:

Here is the js file, it's a simple js that only need to be clicked and then pressed down for 3 seconds.

$(document).ready(function() {
  $('.turbo').on('click', function() {
    document.getElementById('status').innerHTML = 'Turbo clicked';

    $('.accelerate').click(function() {
        alert('accelerate clicked & pressed for 3 seconds');
    });
  });
});

You can use this code below

var pressedDown = false,
clickedBtn = false;
$('button.btn1').click(function(){
    clickedBtn = true;
    $btn1 = $(this);
    $btn1.prop('disabled', true);
    setTimeout(function(){
        $btn1.prop('disabled', false);
    }, 10000);
})

$('button.btn2').on('mousedown', function(e){
    if(clickedBtn){
        pressedDown = true;
        setTimeout(function(){
            if(pressedDown){
                //run code here
            }
        }, 3000);
    }
}).on('mouseup', function(e){
    pressedDown = false;
})

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