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
我希望在使用jquery或javascript的超链接一段时间后撤消event.preventdefault。当我点击我的链接然后它应该重定向到一段时间后在href中给出的链接因为我想在那个时间发送一些ajax请求
Here is my HTML
这是我的HTML
<a href="http://localhost/rightA/en/admin/vacancies/activate/181999/1" class="btn-edit-vacancy"></a>
Here is my javascript
这是我的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我在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 个解决方案
#1
3
The this
keyword in JavaScript confuses new and seasoned JavaScript developers alike.
JavaScript中的this关键字会让新的和经验丰富的JavaScript开发人员感到困惑。
The this Keyword
这个关键字
In JavaScript, the thing called this, is the object that "owns" the JavaScript code.
在JavaScript中,称为this的东西是“拥有”JavaScript代码的对象。
The value of this, when used in a function, is the object that "owns" the function.
在函数中使用时,this的值是“拥有”函数的对象。
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.
对象构造函数中的this关键字没有值。它只是新对象的替代品。
The value of this will become the new object when the constructor is used to create an object.
当构造函数用于创建对象时,this的值将成为新对象。
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
在你的postuserWall函数中
function postuserWall(){
var body = 'Usama New Post';
FB.api('/me/feed', 'post', { message: body }, function(response) {
if (response) {
return true;
} else {
return false;
}
});
}
#2
1
You this
inside setTimeout
refers to the window
object. Instead assign a reference and use it.
你在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);
});
#1
3
The this
keyword in JavaScript confuses new and seasoned JavaScript developers alike.
JavaScript中的this关键字会让新的和经验丰富的JavaScript开发人员感到困惑。
The this Keyword
这个关键字
In JavaScript, the thing called this, is the object that "owns" the JavaScript code.
在JavaScript中,称为this的东西是“拥有”JavaScript代码的对象。
The value of this, when used in a function, is the object that "owns" the function.
在函数中使用时,this的值是“拥有”函数的对象。
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.
对象构造函数中的this关键字没有值。它只是新对象的替代品。
The value of this will become the new object when the constructor is used to create an object.
当构造函数用于创建对象时,this的值将成为新对象。
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
在你的postuserWall函数中
function postuserWall(){
var body = 'Usama New Post';
FB.api('/me/feed', 'post', { message: body }, function(response) {
if (response) {
return true;
} else {
return false;
}
});
}
#2
1
You this
inside setTimeout
refers to the window
object. Instead assign a reference and use it.
你在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);
});