JavaScript callback with `this` confusion callback in this.setstate,javascript callback with this,typescript callback

I have this clock object:

var Clock = {
    start: function () {
      $('#btnScrollPause').show();
      $('#btnScrollResume').hide();

      advance();
      this.interval = setInterval(function () {
        advance();
      }, 5000);
    },

    pause: function () {
      $('#btnScrollPause').hide();
      $('#btnScrollResume').show();
      this.reset();
    },

    resume: function () {
      if (!this.interval) {
        this.start();
      }
    },

    reset: function () {
      clearInterval(this.interval);
      delete this.interval;
    }
  };

I have two buttons that pause and resume automatic scrolling of an element, but my confusion is on the click handlers that I've attached to them.

If I call the pause function like this:

// 1
$('#btnScrollPause').click(function () {
  Clock.pause();
});

It works correctly, however if I try to reduce the code like so:

// 2
$('#btnScrollPause').click(Clock.pause);

It no longer works and I get the error "this.reset is not defined". Also, if I make a function and use that in the click handler instead:

// 3
$('#btnScrollPause').click(scrollPause);

function scrollPause() {
  Clock.pause();
}

It works! Can someone explain why 1 and 3 work, but 2 doesn't?

Answer:1

I'am studying React JS library, and i wish create a "loop" of component, returning array values from json file. Here my json file: { "data": { "children": [{ "data": { "score": 4 ...

I'am studying React JS library, and i wish create a "loop" of component, returning array values from json file. Here my json file: { "data": { "children": [{ "data": { "score": 4 ...

  1. loop component in react
  2. loop component in talend
  3. create react component with loop

I have the following django template and I have defined a button in it so that it calls the javascript function which I added to the page like below. I have the below issues: 1) If I bring the script ...

I have the following django template and I have defined a button in it so that it calls the javascript function which I added to the page like below. I have the below issues: 1) If I bring the script ...

I am trying to make a table using jquery-datatables with the header shown below: <table id="tableau" class="display" width="100%" align="center"> <thead> <tr> <th ...

I am trying to make a table using jquery-datatables with the header shown below: <table id="tableau" class="display" width="100%" align="center"> <thead> <tr> <th ...

  1. issue when using

The explanation is a bit long so please bear with me. I am building a Facebook messenger bot which uses my sails.js/node.js server in the backend and a MongoDB database. In my sails app, I have ...

The explanation is a bit long so please bear with me. I am building a Facebook messenger bot which uses my sails.js/node.js server in the backend and a MongoDB database. In my sails app, I have ...