如何仅在调用函数时添加延迟
How to add delay only when the function is called?
我正在尝试延迟调用函数。
window.setInterval(function(){
//$('.product-display').delay(3000).hide();
document.getElementById('product-list-display').style.display = "none";
},3000);
上面的代码在3秒钟后隐藏div,上面的代码片段在showdiv函数中调用。我需要做的是,我只想在调用showdiv函数时调用上面的延迟函数。。。现在函数每3秒执行一次,即我使用setInterval进行隐藏。但是我只想在调用showdiv时隐藏3秒钟。我该怎么做?我可以使用jquery吗?
function showdiv(city, imagesrc, timeout)
{
window.setTimeout(function() {
document.getElementById('city-order').innerHTML = "";
document.getElementById('order-product').innerHTML = "";
$('.product-display').addClass("zoomin");
document.getElementById('product-list-display').style.display = "block";
var order_placed_city = document.getElementById('city-order');
var content = document.createTextNode(city);
order_placed_city.appendChild(content);
var product_order = document.getElementById('order-product');
var elem = document.createElement("img");
product_order.appendChild(elem);
elem.src = imagesrc;
},timeout);
window.setTimeout(function(){
//$('.product-display').delay(3000).hide();
document.getElementById('product-list-display').style.display = "none";
},3000);
}
向上移动隐藏setTimeout 1行:
function showdiv(city, imagesrc, timeout)
{
window.setTimeout(function() {
document.getElementById('city-order').innerHTML = "";
document.getElementById('order-product').innerHTML = "";
$('.product-display').addClass("zoomin");
document.getElementById('product-list-display').style.display = "block";
var order_placed_city = document.getElementById('city-order');
var content = document.createTextNode(city);
order_placed_city.appendChild(content);
var product_order = document.getElementById('order-product');
var elem = document.createElement("img");
product_order.appendChild(elem);
elem.src = imagesrc;
window.setTimeout(function(){
//$('.product-display').delay(3000).hide();
document.getElementById('product-list-display').style.display = "none";
},3000);
},timeout);
}
尝试使用回调函数来实现这一点。
function showdiv(city, imagesrc, timeout)
{
window.setTimeout(function() {
document.getElementById('city-order').innerHTML = "";
document.getElementById('order-product').innerHTML = "";
$('.product-display').addClass("zoomin");
showDiv(function(){ window.setTimeout(function(){
//$('.product-display').delay(3000).hide();
document.getElementById('product-list-display').style.display = "none";
},3000);});
var order_placed_city = document.getElementById('city-order');
var content = document.createTextNode(city);
order_placed_city.appendChild(content);
var product_order = document.getElementById('order-product');
var elem = document.createElement("img");
product_order.appendChild(elem);
elem.src = imagesrc;
},timeout);
}
function showDiv(callback){
document.getElementById('product-list-display').style.display = "block";
callback();
}
相关文章:
- 为JS函数添加延迟
- 如何为.css状态的更改添加延迟
- 我在向jquery脚本添加延迟时遇到问题
- 如何在使用flash音乐播放器的母版页中自动播放音乐之前添加延迟
- 在添加类之后,为DIPLAY内容添加延迟
- 如何在启动Mocha测试用例之前添加延迟
- 如何添加延迟计时器
- 尝试向 jQuery AJAX 请求添加延迟
- 如何在 AJAX 函数中添加延迟函数
- 如何使用 JQuery 添加延迟?(让它等待 5 秒再继续)
- 如何在引导下拉列表悬停时添加延迟
- 向每个列表项添加延迟的 CSS 动画
- 向 JavaScript 函数添加延迟
- 在“for”循环迭代之间添加延迟
- 使用Javascript向鼠标悬停添加延迟
- 在ajax脚本中添加延迟
- 在Ajax调用后向JQuery keyup()添加延迟
- 在showMessage()之后添加延迟
- 在带有传递给函数的变量的javascript循环中添加延迟
- Javascript为动画添加延迟