Javascript | If语句

Javascript | If语句,javascript,html,Javascript,Html,我不知道javascript,但我想做一些我不知道如何做的事情。在此代码中,这些行不断重复: if (jsonLength==0) { html+='<div class="desc"> <div class="thumb"> <img class="img-circle" src="" width="35px" height="35px" align=""> </div> <div class="details">&

我不知道javascript,但我想做一些我不知道如何做的事情。在此代码中,这些行不断重复:

    if (jsonLength==0)
{

    html+='<div class="desc"> <div class="thumb"> <img class="img-circle" src="" width="35px" height="35px" align=""> </div> <div class="details"><p style="padding-left: 10px;"><a  href="#"></a><br/>Sê o primeiro a escrever no chat!</p></div> </div>';
}
if(jsonLength==0)
{
html+='


Sêo primeiro a escrever无需聊天!

; }
我如何阻止他们重复?完整代码在这里

var lastTimeID = 0;

$(document).ready(function() {
    $('#btnSend').click(function(){
        sendChatText();
        $('#chatInput').val("");
    });
    startChat();
});

function startChat(){
    setInterval(function(){ getChatText(); }, 700);

}

function getChatText(){
    $.ajax({
        type: "GET",
        url: "/refresh.php?lastTimeID="+lastTimeID
    }).done(function( data )
    {
        var jsonData = JSON.parse(data);
        var jsonLength = jsonData.results.length;
        var html = ''; 

        for (var i = 0; i < jsonLength; i++) {
            var result = jsonData.results[i];
            html += '<div class="desc"> <div class="thumb"> <img class="img-circle" src="' + result.picture +'" width="35px" height="35px" align=""> </div> <div class="details"><p style="padding-left: 10px;"><a  href="#">' + result.user_name +'</a><br/>'+result.chattext+ '</p></div> </div>';
            lastTimeID = result.id;
        }
        if (jsonLength==0)
{

    html+='<div class="desc"> <div class="thumb"> <img class="img-circle" src="" width="35px" height="35px" align=""> </div> <div class="details"><p style="padding-left: 10px;"><a  href="#"></a><br/>Sê o primeiro a escrever no chat!</p></div> </div>';
}
        $('#view_ajax').append(html);

    });
}

function sendChatText(){
    var chatInput = $('#chatInput').val();
    if(chatInput != ""){
        $.ajax({
            type: "GET",
            url: "/submit.php?chattext=" + encodeURIComponent( chatInput )
        });
    }
}
var lastTimeID=0;
$(文档).ready(函数(){
$('#btnSend')。单击(函数(){
sendChatText();
$('#chatInput').val(“”);
});
startChat();
});
函数startChat(){
setInterval(函数(){getChatText();},700);
}
函数getChatText(){
$.ajax({
键入:“获取”,
url:“/refresh.php?lastTimeID=“+lastTimeID
}).完成(功能(数据)
{
var jsonData=JSON.parse(数据);
var jsonLength=jsonData.results.length;
var html='';
对于(var i=0;i';
lastTimeID=result.id;
}
如果(jsonLength==0)
{
html+='


Sêo primeiro a escrever无需聊天!

; } $('#view_ajax').append(html); }); } 函数sendChatText(){ var chatInput=$('#chatInput').val(); 如果(聊天输入!=“”){ $.ajax({ 键入:“获取”, url:“/submit.php?chattext=“+encodeURIComponent(chatInput) }); } }
它有一些php部分,但我不认为它们对这个问题有用


提前感谢

把这个放在你的
var html=''下面

if(jsonLength==0)
{
html+='什么都没有!

'; }
这将在JavaScript中对您有所帮助

//Put the following if statment inside the done function
if(数据!=null){
document.getElementById('yourDivID')。insertAdjacentHTML('afterend','无!

'); }
这就是在jQuery中执行此操作的方式。

//将以下if语句放在done函数中
如果(数据!=null){
$(“#yourDivID”)。追加(“无任何内容!

”); }
它不断向div发送垃圾邮件,我该如何修复
//Put the following if statment inside the done function
if (data != null) {
  document.getElementById('yourDivID').insertAdjacentHTML('afterend','<p> Nothing! </p>');
}
//Put the following if statment inside the done function

if (data != null) {
  $('#yourDivID').append('<p> Nothing! </p>');
}