JQuery setTimeout on Append

I am trying to set the timeout of the following add method in jQuery. All I tried returns Uncaught SyntaxError: Unexpected identifier

Here is my code:

  setTimeout("$('#user-comments').append('<div class='video_comment'> <div class='name'>David</div><div class='time'>test</div> <div class='indiv-comment'><p>'"+message+"'</p></div></div><hr class='gray' />')",3000); 

Any help is appreciated.

+4
source share
2 answers
 setTimeout(function () { $("#user-comments").append('<div class="video_comment"> <div class="name">David</div> <div class="time"><?php echo date('F j, Y, g:i a'); ?></div> <div class="indiv-comment"><p>' + message + '</p></div></div>'); }, 3000); 
+7
source

First off, line breaks can be a big problem in javascript. And as Neil said, you need to avoid your quotes. Sometimes breaking up a longer line makes it more readable. Alternatively, you can provide the setTimeout function instead of a string. Here is an example:

 setTimeout(function() { var nameTag = '<div class="name">David</div>'; var timeTag = '<div class="time">test</div>'; var commentTag = '<div class="indiv-comment"><p>' + message + '</p></div>'; var wrap = '<div class="video_comment">' + nameTag + timeTag + commentTag + '</div><hr class="gray" />'; $('#user-comments').append(wrap) }, 3000); 

This way you can clearly see which quotes start and end where you don't have to worry about linear errors inside the string.

0
source

Source: https://habr.com/ru/post/1436378/


All Articles