ajax 刷新后调整大小函数出现问题

Issue with a resize function after ajax refresh

本文关键字:函数 问题 刷新 调整 ajax      更新时间:2023-09-26

我在组合 2 个脚本时遇到问题我有javascript代码来检查我的容器有多大,如果它大于500,它会将内容分为2个不同的div:

function divideStream() {
            if ($("#stream_one_col").width() > 500) {
                var leftHeight = 0;
                var rightHeight = 0;
                $("#stream_one_col").children().each(function() {
                    if (rightHeight < leftHeight) {
                        $("#stream_right_col").append(this);
                        rightHeight += $(this).height();
                        $(this).children().addClass("stream_right_inner");
                    }
                    else {
                        $("#stream_left_col").append(this);
                        leftHeight += $(this).height();
                        $(this).children().addClass("stream_left_inner");
                    }                               
                });
            }
            else {
                $("#content_left").load("php/stream_left.php");
            }
        }

我有一个 ajax 刷新

$(document).ready(
        function() {
            setInterval(function() {
                $('.aroundit').load('php/stream_left.php');
            $(".aroundit").divideStream();
            }, 3000);
        });

现在基本上重新加载就好了但是,函数(divideStream)在激活ajax后不会重新加载。相反,它只是不停地来回跳跃谁能帮我?

函数divideStream不是jQuery的扩展函数。

你应该使用这个:

$(document).ready(
        function() {
            setInterval(function() {
                $('.aroundit').load('php/stream_left.php');
                divideStream();
            }, 3000);
        });

我不太确定我是否理解正在发生的事情。 但我认为您可能希望确保在加载完成后运行divideSteam 函数。 你可以这样做。

 $(document).ready(
    function() {
        setInterval(function() {
            $('.aroundit').load('php/stream_left.php', function() {  //this function runs once load has completed
                divideStream();  // as hugo mentioned   
            });
        }, 3000);
    });