如何在 Javascript 中锁定线程?

发布于 2024-09-16 06:26:13 字数 699 浏览 4 评论 0原文

我想在一个线程运行时锁定其他线程。在.Net 中可以使用锁对象。我需要在 AJAX 中做同样的事情。

    function f_OrnekleriGetir(bKaydet) {

        var ddlKlinikId = "<%=ddlKliniklker.ClientID%>";
        var iKlinikId = $("#" + ddlKlinikId + " option:selected").val();
        var arrOrnekNolar = new Array();
        $(".tdOrnekNo").each(function(idx, td) {
            var sOrnekNo = $(td).html();
            arrOrnekNolar[idx] = sOrnekNo;
        });

        for (var i = 0; i < arrOrnekNolar.length; i++) {

                // i want to wait in here...
                f_OrnekGetirKaydet(arrOrnekNolar[i], iKlinikId, bKaydet);   
            }
        }
    }

是否可以?

KR,

恰金

I want to lock other threads when one thread is running. It is possible in .Net with a lock object. I need do the same in AJAX.

    function f_OrnekleriGetir(bKaydet) {

        var ddlKlinikId = "<%=ddlKliniklker.ClientID%>";
        var iKlinikId = $("#" + ddlKlinikId + " option:selected").val();
        var arrOrnekNolar = new Array();
        $(".tdOrnekNo").each(function(idx, td) {
            var sOrnekNo = $(td).html();
            arrOrnekNolar[idx] = sOrnekNo;
        });

        for (var i = 0; i < arrOrnekNolar.length; i++) {

                // i want to wait in here...
                f_OrnekGetirKaydet(arrOrnekNolar[i], iKlinikId, bKaydet);   
            }
        }
    }

Is it possible?

KR,

Çağın

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

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

发布评论

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

评论(3

高跟鞋的旋律 2024-09-23 06:26:13

您可以用循环“锁定”主线程。不过,这里有一些主要缺点。

  • JavaScript 在浏览器的 UI 线程上运行。这意味着页面的 UI 被锁定,直到当前执行的 JavaScript 完成,从而阻止用户交互和渲染。
  • 某些浏览器会检测到需要很长时间才能完成的脚本,并询问用户是否要停止运行它。
  • 如果浏览器没有响应,操作系统可能会询问用户是否要关闭浏览器。
  • 如果没有可靠的 break; 子句,您可能会陷入无限循环。请记住,在跳出循环之前,不会执行其他代码(甚至回调)。唯一会更改的属性是那些异步更改的属性,例如readyState 属性。

如果您想在等待 Ajax 请求时锁定线程,您可以同步发送请求。然而,这将具有与阻塞循环相同的缺点。

正如 MunkiPhD 所说,您可能正在查看问题不正确。回调在 JavaScript 中被广泛使用,以避免阻塞 UI 线程,几乎可以保证,构建代码以在计时器或事件上使用回调函数会比您当前的想法更好。

You can "lock" the main thread with a loop. There's a few major downsides here, though.

  • JavaScript runs on the browser's UI thread. This means the page's UI is locked up until the currently executing JavaScript completes, blocking user interaction and rendering.
  • Some browsers will detect a script that takes a long time to complete and ask the user if they want to stop running it.
  • The operating system might ask the user if they want to close the browser if it's not responding.
  • Without a solid break; clause, you could get stuck in an infinite loop. Remember that no other code will execute (not even callbacks) until you break out of the loop. The only properties that will change are those that change asynchronously, like the readyState property.

If you want to lock the thread whilst waiting for an Ajax request, you could send the request synchronously. This will have the same downsides as the blocking loop, however.

As MunkiPhD said, you're probably looking at the issue incorrectly. Callbacks are widely used in JavaScript to avoid blocking the UI thread, it's almost guaranteed that structuring your code to use a callback function on either a timer or an event will work much better than what you currently have in mind.

给妤﹃绝世温柔 2024-09-23 06:26:13

你不能在 javascript 中进行多线程处理。都是单线程的。

You can't do multithreading in javascript. It's all single-threaded.

往事风中埋 2024-09-23 06:26:13

我不确定是否可以在 Javascript 中锁定线程,但我认为这可以归结为设计问题。线程应该为可以并行运行的进程运行。如果您希望一个线程等待另一个线程的响应,那么您就错误地看待了这个问题。

不管怎样,您调查过Web Workers吗?

I'm not sure if you can lock threads in Javascript, but I think it boils down to a design issue. Threads are supposed to be run for processes that can run in parallel. If you want a thread to wait for a response from another thread, then you're looking at the issue incorrectly.

Either way, have you investigated Web Workers?

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