像alert()函数一样停止页面执行 [英] Stop page execution like the alert() function

查看:114
本文介绍了像alert()函数一样停止页面执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我写 alert('Hello')时,页面执行停止并等待批准继续。

When I write alert('Hello'), the page execution stops and waits for approval to continue.

我有一个 div 设置,使用HTML显示为假警报 - 这个 div 有一个确定按钮。

I have a div setup to display as a fake alert, using HTML - this div has an 'OK' button.

我希望页面停止执行(就像警报一样),直到用户点击确定。

I want the page to stop its execution (just like alert does) until the user click 'OK'.

是吗可能?

推荐答案

你不能。只有特殊的内置插件才能做到这一点。有一段时间有 showModalDialog 特殊内置功能,允许您为内容指定URI并进行自定义,但它从未得到广泛支持,现在甚至被曾支持它的浏览器也弃用。

You can't. Only the special built-ins can do that. For a while there was the showModalDialog special built-in that let you specify a URI for the content and thus customize it, but it was never widely supported and is now deprecated even by browsers that once supported it.

相反,使用 div 的当前警报功能接受警报关闭时的回调(或返回已解决的承诺)当它关闭时,允许你继续处理。

Instead, make your current alerting function that uses the div accept a callback for when the alert is closed (or return a promise that's settled when it's closed), to allow you to continue processing.

例如,如果你的代码曾经使用 alert 并且像这样工作:

So for instance, if your code used to use alert and work like this:

function foo() {
    var x;

    x = doSomething();
    alert("Alert! Alert!");
    doSomethingAfterTheAlertIsCleared(x);
    doAnotherThingAfterward();
}

...您将其更改为:

...you'd change it to:

function foo() {
    var x;

    x = doSomething();
    fakeAlert("Alert! Alert!", function() {
        doSomethingAfterTheAlertIsCleared(x);
        doAnotherThingAfterward();
    });
}

请注意,现在警报后面的所有代码都在一个函数中,参考我们传入 fakeAlert 。假冒警报仍然显示时, foo 函数返回,但最终用户解除假警报并调用我们的回调。请注意,我们的回调代码可以访问我们正在处理的 foo 调用中的本地人,因为我们的回调是一个闭包(不要担心,如果这是一个相当新的和/或神秘的术语,关闭并不复杂)。

Note that now all the code that followed the alert is in a function, whose reference we pass into the fakeAlert. The foo function returns while the fake alert is still showing, but eventually the user dismisses the fake alert and our callback gets called. Note that our callback code has access to the locals in the call to foo that we were processing, because our callback is a closure (don't worry if that's a fairly new and/or mysterious term, closures are not complicated).

当然,如果警告后面的唯一事情是单个函数调用不带任何参数,我们可以直接传递该函数引用。例如:

Of course, if the only thing following the alert is a single function call that doesn't take any arguments, we could just pass that function reference directly. E.g., this:

function foo() {
    doSomething();
    alert("Alert! Alert!");
    doSomethingAfterTheAlertIsCleared();
}

成为:

function foo() {
    doSomething();
    fakeAlert("Alert! Alert!", doSomethingAfterTheAlertIsCleared);
}

(请注意,没有() doSomethingAfterTheAlertIsCleared 之后 - 我们引用函数对象,而不是调用函数; fakeAlert 会调用它。)

(Note that there are no () after doSomethingAfterTheAlertIsCleared -- we're referring to the function object, not calling the function; fakeAlert will call it.)

如果您不确定 fakeAlert 将如何调用回调,它将在用户关闭警报div的事件处理程序中,并且您只需调用回调的参数,就像对任何其他函数引用一样。因此,如果 fakeAlert 将其作为回调接收,则通过说 callback();来调用它。 / code>。

In case you're not sure how fakeAlert would call the callback, it would be within the event handler for the user "closing" the alert div, and you just call the argument for the callback just like you do with any other reference to a function. So if fakeAlert receives it as callback, you call it by saying callback();.

这篇关于像alert()函数一样停止页面执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆