gpt4 book ai didi

javascript - 函数完成后执行 javascript

转载 作者:行者123 更新时间:2023-11-28 18:40:23 27 4
gpt4 key购买 nike

我想在另一个名为 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com