JavaScript How to access “this” inside a closure? access this inside promise,access this inside arrow function,access

Create a reference to this in slideToggle function.

 jQuery(document).ready(
    function()
    {
        jQuery(".toggle").on("click", function() {
            console.log("let the toggling begin!");

            jQuery(this).slideToggle(600, function(){ // slide up
                var self = this; // <-- notice this
                setTimeout(function(){ // wait 4 sec, then slide back down
                    jQuery(self).slideToggle(600)
                }, 4000);
            });
        });
    }
);
Answer:1

Use bind to specify a this for a function you expect to call out of context.

var foo = {
    bar: function () {
        setTimeout(function () { // though in a setTimeout
            console.log(this);
        }.bind(this), 0); // binding to `this` here means
    }
};

foo.bar(); // invoking it still has `this` of `foo`
Answer:2
var yourThing = jQuery(this);

yourThing.slideToggle(600, function(){ // slide up
     setTimeout(function(){ // wait 4 sec, then slide back down
          yourThing.slideToggle(600)
     }, 4000);
});
Answer:3

Just add this line in your code to understand why:

setTimeout(function(){ // wait 4 sec, then slide back down
      console.log(jQuery(this)); //this one
      jQuery(this).slideToggle(600)
      }, 4000);

Open your console. You will see that, in the setTimeout function, $(this) refers to the window object.

Answer:4

You need a create a referente to this,so when runs the function associate to setTimeout you can pass this reference.

 jQuery(document).ready(
        function()
        {
            jQuery(".toggle").on("click", function() {
                console.log("let the toggling begin!");
                var that = this; // <--- reference to this
                jQuery(this).slideToggle(600, function(){ // slide up
                    setTimeout(function(){ // wait 4 sec, then slide back down
                        jQuery(that).slideToggle(600)
                    }, 4000);
                });
            });
        }
    );
Answer:5

In order to build the final JS source for a web app, I am compiling JavaScript templates, CoffeScript sources and vanilla JS vendor scripts. A final task, dependent on the former three, would then ...

In order to build the final JS source for a web app, I am compiling JavaScript templates, CoffeScript sources and vanilla JS vendor scripts. A final task, dependent on the former three, would then ...

  1. gulp dependent tasks order
  2. gulp 4 dependent tasks

I'm trying to login into Tumblr with CasperJS using methods start, fill and waitForUrl but cannot do this. It seems to me that form wasn't submitted, because timeout in waitForUr expired everytime. ...

I'm trying to login into Tumblr with CasperJS using methods start, fill and waitForUrl but cannot do this. It seems to me that form wasn't submitted, because timeout in waitForUr expired everytime. ...

  1. cannot submit form
  2. cannot submit form javascript
  3. cannot submit form jquery
  4. cannot submit form in safari
  5. cannot submit form laravel
  6. who cannot submit form 15g
  7. tinymce cannot submit form
  8. cannot submit pdf form
  9. powerapps cannot submit form
  10. infopath cannot submit form
  11. cannot submit html form
  12. ajax cannot submit form
  13. ckeditor cannot submit form
  14. infopath cannot submit the form. the parameter is incorrect
  15. infopath cannot submit the form an error occurred
  16. infopath cannot submit the form. access is denied

i'm writing an asp page using vb.net and i need to count the number of characters in my textarea and display the message:"X characters Remaining." that's my asp code: <td valign='top'> ...

i'm writing an asp page using vb.net and i need to count the number of characters in my textarea and display the message:"X characters Remaining." that's my asp code: <td valign='top'> ...

  1. textarea count characters jquery
  2. textarea count characters javascript
  3. textarea count characters
  4. textarea count characters angular
  5. count characters in textarea
  6. html textarea count characters
  7. textarea count remaining characters
  8. textarea javascript count number of characters
  9. bootstrap textarea count characters

I m creating a web tv on a Raspberry Pi Since the RPi will not always be connected to the net, I need to download the file it will have to play before playing them. I can download the file, but when ...

I m creating a web tv on a Raspberry Pi Since the RPi will not always be connected to the net, I need to download the file it will have to play before playing them. I can download the file, but when ...