0

Update:

Sorry about the confusion.

What I'm trying to do is making a web page scroll down to its very end, every 5 seconds, how can I do that?

I tried repeat pressing the end key to get this happen, but I fails.

These were my attempts:

function pressEnd() {
    $('body').keypress({keyCode: 35});
}

setInterval(function () {
    pressEnd();
}, 5000);

And:

$('body').trigger('keydown', {which: 35});

And:

var event = $.Event('keydown', {keyCode: 35});
$('body').trigger(event);

Also tried to change the selector from body to document and window!

What am I missing?

user7607751
  • 465
  • 7
  • 27
  • Possible duplicate of [Is it possible to simulate key press events programatically?](http://stackoverflow.com/questions/596481/is-it-possible-to-simulate-key-press-events-programatically) – Yohanes Gultom Feb 26 '17 at 05:45
  • Are you trying to auto scroll with time interval? Possible duplicate of http://stackoverflow.com/questions/13813257/autoscroll-with-setinterval-clearinterval – vatz88 Feb 26 '17 at 06:07
  • http://stackoverflow.com/a/7318272/3898339 is the solution, you are looking for. modify interval time – Deep 3015 Feb 26 '17 at 06:08

4 Answers4

0

You need to wrap the code in the jQuery ready function like below.

$(document).ready(function() { 
        $('body').keydown(function(event) {
            var keyCode =  event.which;
        alert("You pressed the key code: " +  keyCode );
    });
});

Here is a JSFiddle with the working example. https://jsfiddle.net/txnL8699/

PHELMS
  • 155
  • 11
  • Sorry guys, I wasn't clear enough in my question!. I don't want to click the `end` key for the sake of clicking it, I wanted to click it to do a scroll down by this. I'm so sorry, I updated the question! – user7607751 Feb 26 '17 at 06:04
0

Try this:

var event = $.Event('keydown', {keyCode: 35});

setInterval(function () {
  $('body').trigger(event);
}, 5000);
vatz88
  • 2,422
  • 2
  • 14
  • 25
  • Sorry guys, I wasn't clear enough in my question!. I don't want to click the `end` key for the sake of clicking it, I wanted to click it to do a scroll down by this. I'm so sorry, I updated the question! – user7607751 Feb 26 '17 at 06:04
0

Update

This script will auto scroll down to the bottom of page every 5 sec with animation duration of 500 ms

function scrollDown(el) {
    el.animate({
        scrollTop: el[0].scrollHeight
    }, 500, function() {
        scrollUp(el)
    });
};


 $(document).ready(function() { 
window.setInterval(function(){
  scrollDown($("html,body"));
}, 5000);
})

SEE fiddle : http://jsfiddle.net/BDc6S/240/

Previously

Create a function to send end key and call it every 5 sec using jQuery window.setInterval(function(){})on $(document).ready(function() {}) like

function myFunction() {
var key35 = $.Event("keydown", { keyCode: 35 });
$("body").trigger(key35);
alert("end key pressed");
}

$(document).ready(function() { 
window.setInterval(function(){
  myFunction();
}, 5000);
})

SEE FIDDLE: https://jsfiddle.net/utp2sfm4/

Ambrish Pathak
  • 3,813
  • 2
  • 15
  • 30
  • Sorry guys, I wasn't clear enough in my question!. I don't want to click the `end` key for the sake of clicking it, I wanted to click it to do a scroll down by this. I'm so sorry, I updated the question! – user7607751 Feb 26 '17 at 06:04
  • Thanks a lot! I appreciate it! – user7607751 Feb 26 '17 at 06:21
0

its working fine when put your code inside the ready function.its triggering properly

$(document).ready(function(){ 
setInterval(function () {
    pressEnd();
}, 5000);
});
function pressEnd(){
// Create a new jQuery.Event object with specified event properties.
var e = jQuery.Event("keydown", { keyCode: 35 });
// trigger an artificial keydown event with keyCode 64
jQuery("body").trigger( e );
}
Prem Chand R
  • 41
  • 1
  • 6
  • Sorry guys, I wasn't clear enough in my question!. I don't want to click the end key for the sake of clicking it, I wanted to click it to do a scroll down by this. I'm so sorry, I updated the question! – user7607751 Feb 26 '17 at 06:07