javascript - 函数完成后执行 javascript

标签 javascript jquery callback

我想在另一个名为 loadItems 的 jQuery 函数完成后运行一些 jQuery 代码。 loadItems 函数来自 IAS jQuery 插件 https://github.com/webcreate/infinite-ajax-scroll

如何使用 jQuery 做到这一点

loadItems 函数是:

    /**
     * Loads items from certain url, triggers
     * onComplete handler when finished
     *
     * @param string   the url to load
     * @param function the callback function
     * @param int      minimal time the loading should take, defaults to $.ias.default.loaderDelay
     * @return void
     */
    function loadItems(url, onCompleteHandler, delay)
    {
        var items = [],
            container,
            startTime = Date.now(),
            diffTime,
            self;

        delay = delay || opts.loaderDelay;

        $.get(url, null, function (data) {
            // walk through the items on the next page
            // and add them to the items array
            container = $(opts.container, data).eq(0);
            if (0 === container.length) {
                // incase the element is a root element (body > element),
                // try to filter it
                container = $(data).filter(opts.container).eq(0);
            }

            if (container) {
                container.find(opts.item).each(function () {
                    items.push(this);
                });
            }

            if (onCompleteHandler) {
                self = this;
                diffTime = Date.now() - startTime;
                if (diffTime < delay) {
                    setTimeout(function () {
                        onCompleteHandler.call(self, data, items);
                    }, delay - diffTime);
                } else {
                    onCompleteHandler.call(self, data, items);
                }
            }
        }, 'html');
    }

它在 HTML 中被调用:

<script type="text/javascript">
        jQuery.ias({
        container : '.category-products',
            item: '.products-grid',
            pagination: '.toolbar-bottom',
        next: '.next',
        loader: '<img src="http://example.com/skin/frontend/bootstrapped/default/images/ajax-loader.gif" /> Loading more products, please be patient...',
            onPageChange: function(pageNum, pageUrl, scrollOffset) {
            // This will track a pageview every time the user scrolls up or down the screen to a different page.
            path = jQuery('<a/>').attr('href',pageUrl)[0].pathname.replace(/^[^\/]/,'/');
            _gaq.push(['_trackPageview', path]);
        },
            triggerPageTreshold: 100,
        thresholdMargin: 700,
        trigger: 'Load more items',
        history: true,
        onLoadItems: function(items) {
            jQuery.each(items, function(i, ul){
                jQuery(ul).find('select.qtychange').customSelect()
            });
        }
     });
jQuery(document).ready(function() {
    jQuery().UItoTop({ easingType: 'easeOutQuart' });       
});
</script>

最佳答案

该函数已经提供了回调功能;只需在调用 loadItems 函数时在 onCompleteHandler 参数中提供一个匿名函数即可。像这样的事情:

loadItems('/foo.php', function(data, items) {
    console.log('loading complete');
    console.log(data);
    console.log(items);
});

关于javascript - 函数完成后执行 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36197586/

相关文章:

javascript - 连续滚动。 AJAX、PHP、JAVASCRIPT、MYSQL

javascript - 如何动态添加和删除多个字段?

jquery - 在每次悬停时运行 jQuery slideDown

javascript - 这样的事情在异步代码中有效吗?

c++ - win32下如何设置RichEdit的回调机制

JavaScript - 将 Canvas 文本设为白色

javascript - 使用 JavaScript 计算字符串中的单词数

c# - Kendo UI 输入焦点 - 选择和 Ajax 调用后,DropdownList 保留焦点

javascript - 使用拖动事件在 Canvas 上的图像上绘制矩形

Python时间测量函数