How do we achieve 'setInterval' type behaviour in Server Side Script in a widget?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2022 09:15 AM
Hello,
We need to call a REST API [ accessible within company network boundaries ] from our SN instances. This rest api is reachable via a mid server from the Servicenow instance provided to our company.
We load the data initially from the api & then the requirement is to call this api say every 30 seconds, And represent the updated data in the widget.
I can use $interval in client side script.. But the API blocks it coz of CORS. So far assumption is that this API will block traffic from SN instances.
In the Server side script i can connect to the API via the Outbound -> REST Message's script. How do i replicate the setInterval behavior in Server side?
Thanks.
- Labels:
-
Service Portal Development

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2022 09:49 AM
Try gs.wait(1000)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2022 09:55 AM
Hi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2025 07:42 AM
This is just wrong, he CLEARLY said setInterval, NOT setTimeOut
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-09-2025 02:10 AM
Dunno why this was bumped last week but i thought i might give a reasonable response anyways.
Doing a $interval on client side is not possible as OP stated and having one on the server side wouldn't make a lot of sense anyhow because you somehow need to get this data to the client.
What you can do instead is a server script that looks like this:
(function() {
var restAPI = new myAPIAbstractionScriptInclude();
data.initData = input ? input.initData : restAPI.prepareConnection();
data.apiResult = restAPI.callRestEndpoint(data.initData);
})();
and the client script would look like this:
api.controller=function($interval, spUtil, $scope) {
$interval(spUtil.update.bind(spUtil, $scope), 30000);
};
This will make the the widget updates its data every 30 seconds. You need to have the proper angular data bindings in your 'HTML template', e.g. using {{data.apiResult.something}} to make those updates visible in the HTML.