jQuery 计时 - 想要在方法中添加类似于 DoEvents() 的内容

发布于 2024-10-19 20:19:47 字数 1619 浏览 1 评论 0原文

我有一个包含大约 260 行的数据表。这些行中的每一行都由一个属性(例如parentHeaderName)来标识。当单击该表的标题时,我有一些 jQuery 可以将标题短暂更改为 ajaxload.gif 图像,如下所示,

$('#Header').html('<img src="images/ajaxload.gif"');

然后我通过调用来切换所有 260 行

$([parentHeaderName=something]).toggle();

不幸的是,我无法获取我的部分将标头更改为 ajaxload.gif 的代码才能正常运行。就好像该语句和切换语句同时运行。这意味着用户永远看不到 ajaxload.gif。我可以显示图像的唯一方法是在两个语句之间放置一条警报('...')消息。显然,这不是我们所希望的解决方案。我尝试使用delay()和setTimeout没有效果。我想我需要的是 jQuery 或 javascript 中的某种 DoEvents 等效项,它可以让图像在切换执行之前显示。

有什么想法吗?

谢谢, 克里斯

更新:感谢大家的反馈。这是实际的代码:(

$('.sixdegreestypeheader').click(function() {
    var headerName = $(this).attr("headername");
    var jq = "[headername$=" + headerName + "]";
    var jq2 = "[parentheadername$=" + headerName + "]";

    var originalTextValue = $(jq).text();
    var originalHTMLValue = $(jq).html();

    if (originalHTMLValue.indexOf("collapse") == -1) {
        $(jq).html('<img src="images/collapse.gif" /> ' + originalTextValue);
    }
    else {
        $(jq).html('<img src="images/expand.gif" /> ' + originalTextValue);
    }

    // alert('break here to act as a DoEvents.') // this forces the image change... but I don't want to have to use an alert to force this
    $(jq2).toggle();

    return false;
});

此代码实际上将标头更改为expand.gif或collapse.gif,而不是ajaxload.gif。)

我确实尝试了延迟建议

$(jq2).delay(500).toggle();

,但它没有任何帮助。当需要切换的行数很小时(例如,最多大约 20 行)时,没有问题。但在某些情况下,我需要隐藏一堆(例如 260),就好像浏览器需要几秒钟来浏览 DOM 来完成所有隐藏的事情。这就是为什么我最初希望使用 ajaxload.gif 图像。

谢谢, 克里斯

I have a table of data that contains about 260 rows. Each of these rows is identified by an attribute such as parentHeaderName. When the header of that table is clicked, I have some jQuery in place that briefly changes the header to an ajaxload.gif image, like this

$('#Header').html('<img src="images/ajaxload.gif"');

and then I toggle all 260 rows by calling

$([parentHeaderName=something]).toggle();

Unfortunately, I am not able to get the part of my code that changes the header to the ajaxload.gif to run properly. It is as though both that statement, and the toggle statement, are running all at the same time. Meaning, that the user never sees the ajaxload.gif. The only way I can get the image to show is if I put an alert('...') message between the two statements. Obviously, this is not desired as a solution. I've tried using delay() and setTimeout to no avail. I guess what I need is some sort of DoEvents equivalent in jQuery or javascript that would let the image show prior to the toggle execution.

Any ideas?

Thanks,
Chris

UPDATE: Thanks for the feedback folks. Here's the actual code:

$('.sixdegreestypeheader').click(function() {
    var headerName = $(this).attr("headername");
    var jq = "[headername$=" + headerName + "]";
    var jq2 = "[parentheadername$=" + headerName + "]";

    var originalTextValue = $(jq).text();
    var originalHTMLValue = $(jq).html();

    if (originalHTMLValue.indexOf("collapse") == -1) {
        $(jq).html('<img src="images/collapse.gif" /> ' + originalTextValue);
    }
    else {
        $(jq).html('<img src="images/expand.gif" /> ' + originalTextValue);
    }

    // alert('break here to act as a DoEvents.') // this forces the image change... but I don't want to have to use an alert to force this
    $(jq2).toggle();

    return false;
});

(This code actually changes the header to expand.gif or collapse.gif, rather than ajaxload.gif.)

I did try the delay suggestion

$(jq2).delay(500).toggle();

but it did not help any. When the number of rows that need to get toggled is small - say, up to about 20 - there is no problem. But in some cases, I need to hide a bunch (e.g. 260), and it is as though the browser needs several seconds to chug through the DOM to do all of its hiding of things. That's why I was originally hoping to use the ajaxload.gif image.

Thanks,
Chris

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

豆芽 2024-10-26 20:19:47

感谢大家的帮助。事实证明,问题更多地出在toggle() 语句上,而不是其他任何地方。 .toggle() 处理如此多的记录是导致浏览器挂起的原因,无论我在 .delay()、.when() 等方面做了什么。我最终直接执行了 .hide() 和.show(),这些更改加快了预期行为的速度。为了确定当前的可见性(从而知道是否应该 .hide() 还是 .show()),我检查了第一个匹配元素的可见性,例如 $(criteria:first:visible) 并进行适当的处​​理。

再次感谢,并致以最诚挚的问候。

——克里斯

Thanks for the help everyone. As it turns out, the problem was more with the toggle() statement then anything else. .toggle()ing so many records is what caused the browser to hang for a bit, regardless of what I did in terms of .delay(), .when(), etc. I ended up doing a straight .hide() and .show(), and these changes sped things up to expected behavior. In order to determine the current visibilty (and thus to know whether I should .hide() or .show()), I checked the visibility of the first matched element, e.g. $(criteria:first:visible) and proceeded appropriately.

Thanks again, and best regards.

-- Chris

倾`听者〃 2024-10-26 20:19:47

我的第一个想法是,这种切换发生得太快了,您甚至根本不需要 ajaxload.gif。那么,如果是这样的话,那是一件好事,对吗?

我的第二个想法是如何用任何标题文本重新替换 ajaxload.gif...这个回调何时以及如何发生?

我的第三个想法是,一切都发生得非常快,这很好,但无论如何你仍然想向加载程序显示......如果是这种情况,请将 .delay(500) 或任何时间量放入你的链中,应该让它保持。我觉得还有更多你没有展示的代码,你能展示一下吗?

这是我同时尝试的快速想法......

$( function(){
    var header = $('#Header');
    header.bind('click', function(){
        $(this).html('<img src="images/ajaxload.gif">');
        $([parentHeaderName=something]).delay(500).toggle();
    });
});

现在这实际上会使切换在触发之前等待,因此,您可能希望将延迟放在链上,该延迟发生在您用 ajaxload.gif 替换之前再次标题文本...

我希望这在某种程度上有所帮助,否则请给我更多信息,我很乐意进一步查看。

编辑:

好的,现在我认为这与图像未加载有关,并且警报给这些图像时间加载。因为这些图像首先不在页面上,所以在加载它们之前页面没有引用它们,所以这可能会扰乱你的时间......也许尝试进行加载,然后在回调中设置你的click...如果您使用的是 jquery 1.5,什么时候可能会有所帮助...

$.when( $('<img src="images/collapse.gif" />').load(), $('<img src="images/expand.gif" />').load() ).then( clickSetUp );

clickSetUp 是您的单击功能...

尝试一下,我会在附近:)

My first thought is just that this toggle is happening so fast that you do not even need the ajaxload.gif at all. So, if that is the case, that is a good thing, right?

My second thought is in how you are re-replacing the ajaxload.gif with whatever the header text was...when and how does this callback happen?

My third thought is that everything is happening very quick, which is good, but you still want to show the loader no matter what...if that is the case, throwing a .delay(500) or whatever amount of time into your chain, should make it hold. I feel like there is more code involved that you did not show, could ya show this?

Here is my quick thought to try in the mean time....

$( function(){
    var header = $('#Header');
    header.bind('click', function(){
        $(this).html('<img src="images/ajaxload.gif">');
        $([parentHeaderName=something]).delay(500).toggle();
    });
});

Now this will actually make the toggle wait before firing, so, you might want to put that delay on the chain that happens right BEFORE you replace the ajaxload.gif with the header text again....

I hope this helped in some way, otherwise give me more info and i'd be happy to take a further look.

EDIT:

Ok, now i'm thinking it has to do with the images not being loaded, and the alert is giving these images time to load. Because these images are not on the page in the first place, the page has no reference to them before you load them in, so this could me messing with your timing...maybe trying doing a load, then on the callback, set your click...if you are using jquery 1.5, when might help...

$.when( $('<img src="images/collapse.gif" />').load(), $('<img src="images/expand.gif" />').load() ).then( clickSetUp );

clickSetUp being your click function...

Give it a try, ill be around :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文