Hi,
I am new to jqeury.
Could you please tell me how to get the most of this script. I wrote it by myself and there is quite few repetitions.
- $("textarea[name='storyline']").blur(function() {
- storylineAjaxUpdate();
- eventViewer('Storyline has been saved');
- });
- var strokeCount = 0;
- $("textarea[name='storyline']").keyup(function() {
- strokeCount = ++strokeCount;
- if (strokeCount > 100) {
- storylineAjaxUpdate();
- eventViewer('Storyline has been saved');
- strokeCount = 0;
- }
- });
- function eventViewer(msg) {
- var date = new Date();
- var minutes = date.getMinutes();
- var hours = date.getHours();
- var seconds = date.getSeconds();
- $(".eventManager .menu-title").after("<p>" + msg + "<span class='small'> (" + hours + ":" + minutes + ":" + seconds + ")</span></p>");
- $(".eventManager p:eq(1)").fadeOut(6000);
- }
- function storylineAjaxUpdate(success = '', error = '') {
- var storyline = $("textarea[name='storyline']").val();
- var token = $("input[name='_token']").val();
- $.ajax({
- type: 'PATCH',
- url: "{{ url('/') }}/admin/dealer/add-article/storyline/{{ $articleToEdit->id }}",
- dataType: 'json',
- data: {
- 'storyline': storyline,
- '_token': token
- },
- success: success,
- error: error,
- });
so that is the code. I want to create autosave for my articles and storyline. This script taking care of just the storyline.
as I am new to jquery I would like to know what I can actually improve before writing the article part.
I would also point at the coloured parts.
RED
, I dont like that the variable is out of the block. Can I move it in any way?
BLUE
, I consider this a little bit crazy. Is there any better way how to work with date and time? Couldnt find anything easier.
How can I add 0 before digits of one place e.g. 1, 2, 3...?
Thank you.