【问题标题】:Vue js button freezes domVue js按钮冻结dom
【发布时间】:2016-07-14 20:37:35
【问题描述】:

我正在尝试在按下按钮时切换包含加载动画的跨度,直到函数使用 v-if 完成。但是当我按下按钮时,DOM 冻结并且 span 元素保持不变,直到函数调用结束。如何使 DOM 不冻结并显示加载图标?非阻塞按钮按下可能是一个解决方案?

HTML

    <form class="form-inline">
    <div class="form-group">

        <div style="display: inline-flex" class='input-group date' id='datetimepicker1'>
            <input placeholder="Start Date" id="pick1" type='text' class="form-control"/>
            <span class="input-group-addon">
                <span class="glyphicon glyphicon-calendar"></span>
            </span>
        </div>
    </div>
    <div class="form-group">
        <div style="display: inline-flex" class='input-group date' id='datetimepicker2'>
            <input placeholder="End Date" id="pick2" type='text' class="form-control"/>
            <span class="input-group-addon">
                <span class="glyphicon glyphicon-calendar"></span>
            </span>
        </div>
    </div>
    <div class="form-group">
        <input  class="form-control" type="text" v-model="Keyword" placeholder="keyword">

    </div>

        @*Start Date<input type="text" v-model="StartDate" placeholder="Start Date" style="width: 177px" />
        End Date <input type="text" v-model="EndDate" placeholder="End Date" style="width: 177px" />*@
        <button type="button" v-show="resetShow" class="btn btn-primary btn-lg " v-on:click="reset" id="reset-btn">Reset filters</button>
        <button type="button" v-show="GetShow" type="button" class="btn btn-primary btn-lg " v-on:click="getEdges">Get Edges</button>
        <button   v-show="GetShow" type="button" class="btn btn-primary btn-lg " v-on:click="getNodes">Get Nodes</button>
        <button v-on:click="send" type="button" class="btn btn-default" id="load" >Submit</button>
    <span v-if="loading" id="loader" style="display:none"> <i style="font-size: 197%; color: purple" class='fa fa-circle-o-notch fa-spin'></i> <span style="font-size: 190%"> Processing </span> </span>

Javascript

var vm = new Vue({
el: '#main',
data: {
    resetShow: false,
    Keyword: '',
    StartDate: '2016-06-08T17:03:36.000Z',
    EndDate: '2016-06-16T17:03:36.000Z',
    Dialog: [],
    EdgeList: [],
    NodeList: [],
    loading: false,
    StartDate1: '',
    GetShow: false
},
// define methods under the `methods` object
methods: {

    getById: function(event) {

    },

    send: function(event) {


        this.loading = true;
        console.log(this.StartDate1);
        var StartDate = $('#datetimepicker1').data("DateTimePicker").date().utc().format().split('+')[0]+".000Z";
        var EndDate = $('#datetimepicker2').data("DateTimePicker").date().utc().format().split('+')[0]+".000Z"; 



        if (this.Keyword != null) {

            var g = GetElasticSearch(this.Keyword, StartDate, EndDate);
            s.graph.clear();
            s.graph.read(g);
            sigma.canvas.edges.autoCurve(s);
            s.refresh();
            // Start the ForceLink algorithm:
            sigma.layouts.startForceLink();
            //Louv
            var louvainInstance = sigma.plugins.louvain(s.graph,
            {
                setter: function(communityId) { this.my_community = communityId; }
            });
            var nbLevels = louvainInstance.countLevels();
            console.log(nbLevels);
            var partitions = louvainInstance.getPartitions();
            var nbPartitions = louvainInstance.countPartitions(partitions);
            // Color nodes based on their community
            s.graph.nodes()
                .forEach(function(node) {
                    //console.log(node.my_community);
                    node.color = colors[node.my_community];
                });
            s.refresh({ skipIndexation: true });

            s.graph.nodes()
                .forEach(function(node) {
                    node.color = colors[node.my_community];
                });
            s.refresh({ skipIndexation: true });

            this.loading = true;
        }
    }








}

});

【问题讨论】:

  • 请添加一个jsfiddle

标签: javascript html asynchronous vue.js


【解决方案1】:

当方法完成时,Vue 会异步更新 DOM。

所以你必须让函数中需要很长时间的部分异步,这样它就不会阻塞事件循环。

您可以使用setTimeout(callback, 0) 来异步运行函数。

示例:https://jsfiddle.net/Linusborg/tn8q4sub/

(编辑:这个例子并不总是对我有用,虽然我不太明白为什么会这样 - 试试你的情况)

对于您的代码,应该如下所示:

send: function(event) {

  this.loading = true;

  setTimeout(function () {
    //rest of the send code.
    // and at the end:
    this.loading = false
  }.bind(this),0) // the `bind(this)` is important!
}

【讨论】:

  • 运行超时为 0 的东西不会使函数异步运行,而是暂停 JS 引擎以让渲染/绘画赶上。 stackoverflow.com/questions/779379/…
  • 嗯...不。您链接清楚地指出:“(实际上, setTimeout() 在执行队列的末尾重新排队新的 JavaScript。请参阅 cmets 以获得更长解释的链接。” - 并在调用结束时重新排队堆栈一个异步函数调用。
  • 它确实这么说。但我不同意您的观点,即“在调用堆栈末尾重新排队是一个异步函数调用”,因为堆栈仍将按其最新​​顺序执行,并且可能仍会发生一些阻塞
  • 那么是什么异步函数调用呢?
  • 在这里,真正了解这一点的人使用 setTimeout 来解释“异步回调”:youtu.be/8aGhZQkoFbQ?t=10m35s
猜你喜欢
  • 2021-06-24
  • 2021-07-04
  • 1970-01-01
  • 2012-01-11
  • 1970-01-01
  • 2013-03-15
  • 1970-01-01
  • 2016-12-18
  • 1970-01-01
相关资源
最近更新 更多