简体   繁体   中英

Show information into div with ajax setTiemout

I have another doubt about Ajax and Jquery I need to append the code I recieve correctly since database , but when I put the code into div #datos the code repeat everytime , I don't need that code was repeat everytime.

setInterval(function() {                  
        $.ajax({
            async:true,
           type: "GET",
           url:"obtenercomentarios",
           success:obtener,
           timeout:4000
       });
         }, 1000);


function obtener(response){
    $.each(response, function( index, value ) {
      $( "#datos" ).append(value.emisor);
      $( "#datos" ).append(value.contenido);

    });
}

An image is better than 1000 words in this case

在此处输入图片说明

Solution: Don't use .append() use .html() instead, or if you want the divs to have some static text before the appended text just use a span inside the divs right after the static text and use .html() , that case every time the code get executed, the span contents will be replaced with the new contents. Example:

<div id="datos">
Hi there <span id="mySpan"></span>
</div>

$( "#mySpan" ).html(value.emisor);

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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