简体   繁体   English

jQuery功能完成后发出警报

[英]Alert After jQuery Function is Finished

I'm trying very hard to figure out how to get a JS alert to appear only after a previous script ( a glossary word replacement script ) has finished. 我正在努力找出如何使JS警报仅在上一个脚本( 词汇表单词替换脚本 )完成后才出现。 Getting this javascript/jquery to run in order is giving me a headache as it either runs out of order, or the second part doesn't run at all. 让此javascript / jquery按顺序运行让我头疼,因为它要么顺序混乱,要么第二部分根本无法运行。

Here is the code that is presently working WITHOUT the alert: 这是当前没有警报的代码:

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script src="jquery.zglossary.min.js" type="text/javascript"></script>

<script>
$(document).ready(function () {
    $('body').glossary('listofwords.json');
    //want alert('finished') to happen after glossary word replacement is finished replacing words here
});
</script>

</head>

I've tried a lot of different things trying to get an alert to happen where that comment is, and either the alert happens immediately before any words are replaced by the zglossary script, an example of that would be: 我尝试了很多不同的尝试,试图使警报发生在该注释所在的位置,或者警报在zglossary脚本替换任何单词之前立即发生,例如:

<script>
$(document).ready(function () {
    $('body').glossary('listofwords.json');
    alert('finished');
});
</script>

Or the script will replace all the words and no alert happens at all. 否则脚本将替换所有单词,并且根本不会发出警报。 An example of that would be: 一个例子是:

<script>
$(document).ready(function () {
    $('body').glossary('listofwords.json');
}, function() {
    alert('finished');
});
</script>

Another example of that happening (words are replaced, but no alert happens) that I've tried would be: 我尝试过的另一种情况(替换了单词,但没有警报发生)是:

<script>
$(document).ready(function () {
    $('body').glossary('listofwords.json', function() {alert('finished');} );
});
</script>

I really just can't figure this out. 我真的无法解决这个问题。 I'd appreciate any help 我将不胜感激

I've never used this glossary plugin before - but I downloaded the source and I see the issue is clearly because the glossary plugin does not provide any callback after the JSON data is asynchronously downloaded. 我以前从未使用过这个词汇表插件-但是我下载了源代码,我看到的问题很明显,因为词汇表插件在异步下载JSON数据后不提供任何回调。 Any solution that is not hacky will require a slight modification to the library itself. 任何非骇客的解决方案都需要对库本身进行一些修改。

Here is a quick way you could do it 这是您可以做到的快速方法

By adding this line 通过添加此行

typeof options.callback == 'function' && options.callback();

At the end of the success: function(data) { success: function(data) {结束时success: function(data) {

Then your code would simply be this: 那么您的代码就是这样:

<script>
$(document).ready(function () {
    $('body').glossary('listofwords.json', {callback:function() {alert('finished');}} );
});
</script>

Which is similar to your last example, only it puts the callback into an options JSON which is what the plugin expects. 这与您的上一个示例类似,只是将回调函数放入了插件期望的选项JSON中。

I have no idea what's "Glossary", but you might find a solution with Deferred object supplied by jQuery api . 我不知道什么是“词汇表”,但是您可能会找到jQuery api提供的Deferred对象的解决方案。 Deferred helps to handle json calls, especially the .done() method to set a caalback when the call is finished. Deferred帮助处理json调用,尤其是.done()方法,以在调用结束时设置回音。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM