如何使用 jquery 或 javascript 在超链接上一段时间后撤消 event.preventdefault

how to undo event.preventdefault after some time on hyperlink using jquery or javascript

我想在 hyperlink 上使用 jquery 或 javascript 一段时间后撤消 event.preventdefault。 当我点击我的 link 然后它应该在一段时间后将我重定向到 href 中给出的 link 因为我想在那个时候发送一些 ajax 请求

这是我的HTML

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

这是我的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);

});

设置超时功能正在运行,但一段时间后它没有将我重定向到所需的 link,而是将我重定向到此 link

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

下面的我也试过了

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

这是我的函数 postuserWall 我在 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;
          }
        });
    }

支票显示我在警报中未定义 谢谢

JavaScript 中的 this 关键字让新手和经验丰富的 JavaScript 开发人员都感到困惑。

this关键字

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;
    }
});

在你的 postuserWall 函数中

 function postuserWall(){
        var body = 'Usama New Post';
        FB.api('/me/feed', 'post', { message: body }, function(response) {
          if (response) {
            return true;
          } else {
            return false;
          }
        });
    }

this里面setTimeout指的是window对象。而是分配一个引用并使用它。

$('.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);

});