如何在调用函数时添加延迟?

时间:2015-07-02 06:30:58

标签: javascript jquery

我试图延迟调用该函数。

       window.setInterval(function(){

        //$('.product-display').delay(3000).hide();
        document.getElementById('product-list-display').style.display = "none";

       },3000);

上面的代码在3秒后隐藏了div,在show div函数中调用了上面的代码片段。我需要做的是,我只想在调用show div函数时调用上面的延迟函数...现在函数每3个执行一次,即我使用setInterval进行隐藏。但我只想在show div被调用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);
}

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

尝试使用回调函数来实现此目的。

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