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

How to add delay only when the function is called?

本文关键字:添加 延迟 函数 调用 何仅      更新时间:2023-09-26

我正在尝试延迟调用函数。

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