admin管理员组文章数量:1425717
I have the following code which uses Jquery UI Drag and Drop. When the item is dropped into the area, the drop zone changes into the pany logo, then I want a delay and a redirect to the URL within the dropped link.
I can get the logo to change OR the url to redirect but not both, when I have both setup the delay doesn't happen and the redirect goes start away. I assume I am doing something wrong with the setTimeout.
Code is as follows:
// let the trash be droppable, accepting the gallery items
$( "#droparea" ).droppable({
accept: "ul.gallery > li a",
activeClass: "ui-state-highlight",
drop: function( event, ui ) {
var thelink = $(ui.draggable).attr("href");
$('#droparea').prepend('<img id="theImg" src="../img/logo_navigation.jpg" />');
setTimeout(redirectLink(url),5000);
}
});
// URL REDIRECT FUNCTION
function redirectLink(url)
{
window.location.replace(url);
}
I have the following code which uses Jquery UI Drag and Drop. When the item is dropped into the area, the drop zone changes into the pany logo, then I want a delay and a redirect to the URL within the dropped link.
I can get the logo to change OR the url to redirect but not both, when I have both setup the delay doesn't happen and the redirect goes start away. I assume I am doing something wrong with the setTimeout.
Code is as follows:
// let the trash be droppable, accepting the gallery items
$( "#droparea" ).droppable({
accept: "ul.gallery > li a",
activeClass: "ui-state-highlight",
drop: function( event, ui ) {
var thelink = $(ui.draggable).attr("href");
$('#droparea').prepend('<img id="theImg" src="../img/logo_navigation.jpg" />');
setTimeout(redirectLink(url),5000);
}
});
// URL REDIRECT FUNCTION
function redirectLink(url)
{
window.location.replace(url);
}
Share
Improve this question
asked May 15, 2013 at 21:12
sluggerdogsluggerdog
8434 gold badges12 silver badges38 bronze badges
1 Answer
Reset to default 10Explanation
You need to pass a function reference (or a string of JavaScript code) to setTimeout
, in order for it to be executed when the timeout is reached.
In your code, you were immediately calling the function (which doesn't return
anything anyways, so its return value is undefined
...so nothing will execute when the timeout is reached).
Approach 1
Use an anonymous function that runs your code:
setTimeout(function () {
redirectLink(url);
}, 5000);
function redirectLink(url) {
window.location.replace(url);
}
Approach 2
Make your redirectLink
function return a function and call it like you originally did:
setTimeout(redirectLink(url), 5000);
function redirectLink(url) {
return function () {
window.location.replace(url);
};
}
Approach 3
You could use .bind()
:
setTimeout(redirectLink.bind(null, url), 5000);
function redirectLink(url) {
window.location.replace(url);
}
Note that .bind()
requires a polyfill for some older browsers.
References:
setTimeout
: https://developer.mozilla/en-US/docs/DOM/window.setTimeoutFunction.prototype.bind
: https://developer.mozilla/en-US/docs/JavaScript/Reference/Global_Objects/Function/bind
本文标签: javascriptGetting setTimeout to workStack Overflow
版权声明:本文标题:javascript - Getting setTimeout to work - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745411046a2657470.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论