我在javascript函数中有一些jQuery,可以更改页面上的文本,并在特定的时间间隔内淡入淡出.我希望函数在每个函数完成其效果之后依次运行.
dialogueExchange1();
dialogueExchange2();
dialogueExchange3();
function dialogueExchange1() {
$('.text-area1').text("hey");
$('.text-area1').delay(1000).showDialogue(800, 3000).prepareDialogue(800, "hey, are you awake?");
}
function dialogueExchange2() {
$('.text-area1').delay(900).showDialogue(800, 4000).prepareDialogue(800, "wake up").delay(900);
$('.text-area2').text("...");
$('.text-area2').delay(1200).showDialogue(800, 1500).fadeOut(800);
}
function dialogueExchange3() {
$('.text-area1').delay(900).showDialogue(800, 4000).prepareDialogue(800, "let's go").delay(900);
$('.text-area2').text("not yet");
$('.text-area2').delay(1200).showDialogue(800, 1500).fadeOut(800);
}
Run Code Online (Sandbox Code Playgroud)
该showDialogue和prepareDialogue是我创建的延迟的方法和淡入淡出的文本.这工作正常.基本上,我只是想在特定时间之后在文本区域选择器中更改文本.目前正在发生的是所有功能同时运行,从而同时触发文本更改效果.我想做dialogueExchange1它的效果,当它完成,为了dialogueExchange2做它的效果,然后当它完成,等等.
我已经尝试通过下面的解决方案来处理队列,超时和回调,但我还没有完全按照我的意愿去做:
过去我有这个工作,只需将所有文本更改方法链接在一行代码中就可以完成我想要的工作,但它看起来很糟糕.将它分解为函数并按顺序运行它会使它更有条理,更有助于跟踪文本更改和延迟时间.谢谢!
编辑:showDialogue和prepareDialogue按要求的功能
$.fn.showDialogue = function(fadeInTime, showTextTime) {
this.fadeIn(fadeInTime).delay(showTextTime);
return this;
};
$.fn.prepareDialogue = function(fadeOutTime, dialogue) {
this.fadeOut(fadeOutTime, function() {
$(this).html(dialogue);
});
return this;
};
Run Code Online (Sandbox Code Playgroud)
解决方案编辑2:感谢大家的回应,并首先建议使用 …
我目前正在构建一个API并尝试测试我的控制器.我收到了与Devise相关的错误,我无法解决.这是我目前拥有的:
# /spec/requests/api/v1/sessions_spec.rb
require 'spec_helper'
describe API::V1::SessionsController do
describe "POST /sessions" do
it "on success, returns the user's authentication token" do
user = FactoryGirl.create(:user)
user_params = {
"user" => {
"email" => user.email,
"password" => user.password
}
}.to_json
post "/api/v1/sessions", user_params
expect(response.status).to eq(200)
json = JSON.parse(response.body)
expect(json['authentication_token']).to eq(user.authentication_token)
end
end
end
# /app/controllers/api/v1/sessions_controller.rb
class API::V1::SessionsController < Devise::SessionsController
respond_to :json
def create
end
end
# routes.rb
MyApplication::Application.routes.draw do
namespace :api, defaults: { format: :json } do
namespace :v1 do
devise_scope :user …Run Code Online (Sandbox Code Playgroud)