【问题标题】:How to add a lazy load to the script?如何向脚本添加延迟加载?
【发布时间】:2017-10-25 22:41:37
【问题描述】:

有一个脚本:

<script>
    $(function (){
        $('#jstree')
            .jstree({
                "plugins": [ "dnd", "sort", "json_data" ],
                'core':{
                    "check_callback" : true,
                    "plugins" : ["contextmenu", "dnd"],
                    'data': {
                        'url': function  (node) {
                            return node.id === '#' ? 'ajax?id=root' : 'ajax?id=' + node.id;
                        },
                        'data':  function (node) {
                            return { 'id': node.id };
                        }
                    }
                }
            });
    });

</script>

我需要在打开节点上添加延迟(2 秒)。我阅读了 jQuery 文档,但在那里没有找到答案。请帮我如何添加延迟?

【问题讨论】:

  • Java != Javascript

标签: javascript jquery jstree


【解决方案1】:

从其他两个 SO answers. 组合中引用的答案

本质上,在 DOM 加载时设置超时功能,并在运行时创建 &lt;script&gt; 标签。然后只需将其附加到 &lt;body&gt; 即可加载。

console.log("Seconds: " + new Date().getSeconds());
setTimeout(function() {

  var blob = new Blob(["console.log('Loaded after')"]); // Insert JS code into []
  var script = document.createElement('script');
  var url = URL.createObjectURL(blob);
  script.onload = script.onerror = function() {
    URL.revokeObjectURL(url);
  };
  script.src = url;
  document.body.appendChild(script);
  console.log("Seconds: " + new Date().getSeconds());
}, 2000);
<html>

<head>
</head>

<body>
  <p>foo</p>
</body>

</html>

【讨论】:

    猜你喜欢
    • 2023-04-05
    • 2012-03-25
    • 2014-01-26
    • 2020-09-22
    • 1970-01-01
    • 2010-10-06
    • 2019-12-15
    • 1970-01-01
    • 2015-09-07
    相关资源
    最近更新 更多