Extend JavaScript Function with Additional Functionality: Example with AJAX
To extend the loadqposts function with another function, you can modify the function definition to accept an additional parameter for the new function and then call that function within the success callback of the AJAX request. Here's an example of how you can do it:\n\njavascript\nfunction loadqposts(getdata, additionalFunction) {\n\t\n\t $.ajax({\n type: 'POST',\n url: qqajaxurl,\n data: {'action' : 'qq_posts_loadX','urldata' : getdata},\n dataType: 'html',\n\t beforeSend : function () {\t\n\n\t\tif ($('.loadingis').length === 0) {\n\t\t\t$('.paging-content').append('<div class="loadingis"><div class="infinityChrome"> <div></div> <div></div> <div></div> </div></div>');\n\t\t}\t\n\t \n\t\t$('.noproduct').hide();\n\t },\n success: function(d_page) {\n\t\t \n\t\t$('.loadingis').remove();\n\t\t$('.paging-content').html(d_page);\n\t\t$('.qloop_content_fake').hide();\n\t\t$('.qloop_content').show();\n\t\t$('.paging-content').removeClass('qblurr');\n\t\t\n\t\t// Call the additionalFunction here\n\t\tadditionalFunction();\n\t\t\n\t\treturn false;\n },\n error: function(d_page){\n\t\t$('.loadingis').remove();\n\t\t$('.paging-content').removeClass('qblurr');\n\n\t\tconsole.log(d_page);\n\t\treturn false;\n },\n });\n\t\n\treturn false;\t\n\t\n}\n\n// Example usage\nfunction additionalFunction() {\n\tconsole.log("This is the additional function");\n}\n\nloadqposts(getdata, additionalFunction);\n\n\nIn this example, the loadqposts function now accepts an additional parameter additionalFunction, which is the function you want to extend with. Inside the success callback, after updating the content and removing the loading animation, the additionalFunction is called.
原文地址: https://www.cveoy.top/t/topic/p1io 著作权归作者所有。请勿转载和采集!