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

Javascript 如何仅在调用函数时添加延迟?,javascript,jquery,Javascript,Jquery,我试图延迟调用该函数 window.setInterval(function(){ //$('.product-display').delay(3000).hide(); document.getElementById('product-list-display').style.display = "none"; },3000); 上面的代码在3秒钟后隐藏了div,上面的代码段在show div函数中被调用。我需要做的是,我只想在

我试图延迟调用该函数

       window.setInterval(function(){

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

       },3000);
上面的代码在3秒钟后隐藏了div,上面的代码段在show div函数中被调用。我需要做的是,我只想在调用show div函数时调用上面的delay函数…现在函数每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);
}

向上移动隐藏设置超时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();
}

您可以在任何地方调用setTimeout,包括在其他函数中。粘贴的上述代码应执行文档。getElementById'product-list-display'。style.display=none;调用shodiv后的3秒代码。到底是什么不起作用?对不起,问题真的不清楚。。在showdiv中使用setTimeout,在上面的代码段中使用setInterval。。另外,名为showdiv的函数仅隐藏元素\u product-list-display\u。。。