Javascript 如何返回到随机文本生成器函数的开头?

Javascript 如何返回到随机文本生成器函数的开头?,javascript,generator,Javascript,Generator,我使用数组构建了这个随机文本生成器,但是用户只能每30秒单击一次按钮,并且在此期间禁用。 当显示阵列中的项目时,在禁用的功能完成后,如何淡出显示问题“您的名字是什么?” 生成文本 你叫什么名字? function GetValue() { var myarray= new Array("Item1", "item2", "item3" ); var random = myarray[Math.floor(Math.random() * myarray.length)];

我使用数组构建了这个随机文本生成器,但是用户只能每30秒单击一次按钮,并且在此期间禁用。 当显示阵列中的项目时,在禁用的功能完成后,如何淡出显示问题“您的名字是什么?”


生成文本

你叫什么名字?

function GetValue() {
    var myarray= new Array("Item1", "item2", "item3" );
    var random = myarray[Math.floor(Math.random() * myarray.length)];
    //alert(random);
    document.getElementById("message").innerHTML=random;
} 

$(function() {
    $("button").click(function() {
        $("button").attr("disabled", "disabled");
        setTimeout(function() {
            $("button").removeAttr("disabled");      
        }, 30000);
    });
});

您可以这样做:

var question=document.getElementById(“message”).innerHTML;
函数changeTextAndFade($element,text){
$element.fadeOut(函数(){
$(this.text(text).fadeIn();
})
}
函数GetValue(){
var myarray=新数组(“Item1”、“item2”、“item3”);
var random=myarray[Math.floor(Math.random()*myarray.length)];
changeTextAndFade($(“#消息”),随机;
}
$(函数(){
$(“按钮”)。单击(函数(){
$(“按钮”).attr(“禁用”、“禁用”);
setTimeout(函数(){
$(“按钮”).removeAttr(“禁用”);
changeTextAndFade($(“#消息”),问题);
}, 3000);
});
});

生成文本

你叫什么名字?

类似这样的名字。您不需要额外的单击事件处理程序

function GetValue() {
    var myarray= new Array("Item1", "item2", "item3" );
    var random = myarray[Math.floor(Math.random() * myarray.length)];
    //alert(random);
    document.getElementById("message").innerHTML=random;
} 

$(function() {
    $("button").click(function() {
        $("button").attr("disabled", "disabled");
        setTimeout(function() {
            $("button").removeAttr("disabled");      
        }, 30000);
    });
});
function GetValue() {
    var myarray= new Array("Item1", "item2", "item3" );
    var random = myarray[Math.floor(Math.random() * myarray.length)];
    $("#message").html(random);
    $("button").prop('disabled', true);
    setTimeout(function() {
        //don't use removeAttr, this completely removed the attribute fromt the element. meaning it cant be true.
        $("button").prop('disabled', false);        
        $("#message").hide(0).html('What is your name?').fadeIn();
    }, 30000);
} 

<!-- html--> 
<button type="button" id="btnSearch" href="#" class="btn btn-block tellme"  onclick="GetValue();" > generate text </button>

<p id="message"  >What is your name?</p> 
函数GetValue(){
var myarray=新数组(“Item1”、“item2”、“item3”);
var random=myarray[Math.floor(Math.random()*myarray.length)];
$(“#消息”).html(随机);
$(“按钮”).prop('disabled',true);
setTimeout(函数(){
//不要使用removeAttr,这将完全删除元素的属性。这意味着它不可能是真的。
$(“按钮”).prop('disabled',false);
$(“#message”).hide(0.html('你叫什么名字?').fadeIn();
}, 30000);
} 
生成文本

你叫什么名字?

function GetValue() {
    var myarray= new Array("Item1", "item2", "item3" );
    var random = myarray[Math.floor(Math.random() * myarray.length)];
    //alert(random);
    document.getElementById("message").innerHTML=random;
} 

$(function() {
    $("button").click(function() {
        $("button").attr("disabled", "disabled");
        setTimeout(function() {
            $("button").removeAttr("disabled");      
        }, 30000);
    });
});

我正在努力解决淡入淡出的问题,如何实现这一点?我的答案是你如何做到这一点。先隐藏,然后显示。