M
M
Mikhail Petrov2015-08-28 05:46:14
JavaScript
Mikhail Petrov, 2015-08-28 05:46:14

How to load JS file after some time interval?

Greetings!
Faced such a problem.
One-page online service in AngularJS.
In the first 10-15 seconds after loading, there is a very active interaction with the server -
JS is working, loading the data necessary for display.
Put Livesite. It starts downloading at the same time as my requests.
It is extremely important that JS gets everything it needs from the server as quickly as possible. And Zhivosayt gets confused under his feet with his download.
It is necessary that Live Site starts loading in 15-20 seconds after all components are loaded.
Is that possible?
I'm not asking as an expert. In order for the programmer to poke into a link or your answer and say "do it."

Answer the question

In order to leave comments, you need to log in

5 answer(s)
S
Sergey Melnikov, 2015-08-28
@Emike

function loadScript(src) {
  var s = document.createElement('script')
  s.src = src
  document.body.appendChild(s)
}

// и дальше в нужном месте вызываем
loadScript('path/to/my/script.js');

// или по таймауту
setTimeout(function() { loadScript('path/to/my/script.js') }, 15000);

N
Nikita Baev, 2015-08-28
@drugoi

Through $timeoutYou can do initializationIt is possible to load jivosite by callback when all requests are processed.

A
Alexey P, 2015-08-28
@ruddy22

$scope.$watch(function() {
    return $http.pendingRequests.length;
}, function(newVal) {
    if (newVal === 0) {
      (function loadScript(src) {
         var scriptElement = document.createElement('script');
         scriptElement.src = src;
         window.document.head.appendChild(scriptElement);
      })("/path/to/script/");
    }
});

well, a few pictures of
the code
created the element and an attempt to load the script
element in the tree

A
Alexander Prozorov, 2015-08-28
@Staltec

I won’t give a link, but you can dig towards using a combination of setTimeout () and JQuery .getScript () method

M
Mikhail Petrov, 2015-08-29
@Emike

Here is what LiveSite support replied:
1) You need to wrap our script in setTimeout
2) admin.jivosite.com - settings - options - reload (enable),
3) There will be an update in October that will not block loading window

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question