2

I want to my undo event.preventdefault after some time on hyperlink using jquery or javascript. when I click on my link then it should redirect me to the link given in href after some time because i want to send some ajax request in that time

Here is my HTML

<a href="http://localhost/rightA/en/admin/vacancies/activate/181999/1" class="btn-edit-vacancy"></a>

Here is my javascript

$('.vacancies_tbl').on('click', '.btn-edit-vacancy', function(e) {
    e.preventDefault();

    var check = postuserWall();
    alert(check);
    setTimeout(function(){window.location = $(this).attr('href'); }, 4000);

});

the set timeout function is working but after some time it does not redirect me to desired link but it redirect me to this link

http://localhost/rightA/en/admin/vacancies/index/undefined

I have also tried the follwing

setTimeout(function(){$(this).trigger("click"); }, 5000);
setTimeout(function(){alert('sdadsa'); $(this).unbind('click') }, 5000);

Here is my function postuserWall i am working on facebook javascript api

postuserWall(){
        var body = 'Usama New Post';

        FB.api('/me/feed', 'post', { message: body }, function(response) {
          if (!response || response.error) {
            console.log(response);
            alert('Error occured');
            return false;
          } else {
            alert('Post ID: ' + response.id);
            return true;
          }
        });
    }

the check show me undefined in the alert Thanks

2 Answers2

3

The this keyword in JavaScript confuses new and seasoned JavaScript developers alike.

The this Keyword

In JavaScript, the thing called this, is the object that "owns" the JavaScript code.

The value of this, when used in a function, is the object that "owns" the function.

The value of this, when used in an object, is the object itself.

The this keyword in an object constructor does not have a value. It is only a substitute for the new object.

The value of this will become the new object when the constructor is used to create an object.

var href;  // globally defined

$('.vacancies_tbl').on('click', '.btn-edit-vacancy', function(e) {
    e.preventDefault();
    href = $(this).attr('href');
    var check = postuserWall();
    if(check){
       window.location = href;
    }
});

In your postuserWall function

 function postuserWall(){
        var body = 'Usama New Post';
        FB.api('/me/feed', 'post', { message: body }, function(response) {
          if (response) {
            return true;
          } else {
            return false;
          }
        });
    }
Sudharsan S
  • 15,336
  • 3
  • 31
  • 49
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/79441/discussion-on-answer-by-jqueryking-how-to-undo-event-preventdefault-after-some-t). – Taryn Jun 02 '15 at 16:20
1

You this inside setTimeout refers to the window object. Instead assign a reference and use it.

$('.vacancies_tbl').on('click', '.btn-edit-vacancy', function(e) {
    e.preventDefault();
    var $this = $(this); //Assigned a reference
    postuserWall();
    setTimeout(function(){window.location = $this.attr('href'); }, 4000);

});
Zee
  • 8,420
  • 5
  • 36
  • 58
  • what about if i dont want to use window.location.href = hrefLink; and want to restore the default behaviour after some time in setTimeoutfunction of the a href when postuserWall() is returned true or false – Muhammad Usama Mashkoor Jun 02 '15 at 07:59