在重新开始之前等待功能完成 [英] Wait for function to finish before starting again

查看:76
本文介绍了在重新开始之前等待功能完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好,

每次用户按下按钮时,我都会尝试调用相同的功能。以下是目前发生的情况..

I am trying to call the same function everytime the user presses a button. Here is what happens at the moment..

用户点击按钮 - >调用函数 - >函数需要1000ms +才能完成(由于使用jQuery和AJAX调用动画)

User clicks button -> Calls function -> function takes 1000ms+ to finish (due to animation with jQuery and AJAX calls)

我想要发生的是每次用户按下按钮时它会将功能添加到队列中,等待上一次调用完成,然后启动..

What I want to happen is every time the user presses the button it adds the function to the queue, waits for the previous call to finish, and then starts..

这可能吗?

很抱歉,如果我的解释有点令人困惑..

Sorry if my explanation is a bit confusing..

谢谢Matthew

推荐答案

由于函数是对象,因此它们可以存储属性。你可以让函数在每次调用(按下按钮)时自动增加一个'queue'属性,然后在函数执行结束时,检查它自己的队列属性是否> 0,如果是,则减少队列属性一个并使用 setTimeout 再次调用自己。

Since Functions are objects they can store properties. You can have your function increment a 'queue' property on itself every time its called (button pressed) and then at the end of the function execution, check if the queue property of itself is > 0, and if so, decrement the queue property by one and call itself again using setTimeout.

这篇关于在重新开始之前等待功能完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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